diff --git a/modules/paragraphs_library/config/schema/paragraphs_library.schema.yml b/modules/paragraphs_library/config/schema/paragraphs_library.schema.yml index 70ff22b..33581e5 100644 --- a/modules/paragraphs_library/config/schema/paragraphs_library.schema.yml +++ b/modules/paragraphs_library/config/schema/paragraphs_library.schema.yml @@ -2,6 +2,6 @@ paragraphs.paragraphs_type.*.third_party.paragraphs_library: type: mapping label: 'Per-paragraph type third party settings' mapping: - disallow_library_conversion: + allow_library_conversion: type: integer - label: 'Disallow conversion to paragraph library item' + label: 'Allow conversion to paragraph library item' diff --git a/modules/paragraphs_library/paragraphs_library.module b/modules/paragraphs_library/paragraphs_library.module index 75b6ba2..460826c 100644 --- a/modules/paragraphs_library/paragraphs_library.module +++ b/modules/paragraphs_library/paragraphs_library.module @@ -28,8 +28,8 @@ function paragraphs_library_paragraph_widget_dropbutton_alter(&$links, &$context $bundle = $context['paragraphs_entity']->bundle(); /** @var ParagraphsTypeInterface $paragraphs_type */ $paragraphs_type = $context['paragraphs_entity']->getParagraphType(); - $disallow_library_conversion = $paragraphs_type->getThirdPartySetting('paragraphs_library', 'disallow_library_conversion'); - if (!$disallow_library_conversion && $bundle != 'from_library') { + $allow_library_conversion = $paragraphs_type->getThirdPartySetting('paragraphs_library', 'allow_library_conversion'); + if ($allow_library_conversion && $bundle != 'from_library') { $id_prefix = implode('_', array_merge($parents, array($field_name, $context['delta']))); $links['add_to_library'] = [ '#value' => t('Add to library'), @@ -148,10 +148,10 @@ function paragraphs_library_form_paragraphs_type_form_alter(&$form, FormStateInt // Adds paragraph type grouping to the form. /** @var \Drupal\paragraphs\ParagraphsTypeInterface $paragraph_type */ $paragraph_type = $form_state->getFormObject()->getEntity(); - $form['disallow_library_conversion'] = [ + $form['allow_library_conversion'] = [ '#type' => 'checkbox', - '#title' => t('Do not allow adding to library'), - '#default_value' => $paragraph_type->getThirdPartySetting('paragraphs_library', 'disallow_library_conversion', FALSE), + '#title' => t('Allow adding to library'), + '#default_value' => $paragraph_type->getThirdPartySetting('paragraphs_library', 'allow_library_conversion', FALSE), ]; $form['#entity_builders'][] = 'paragraphs_library_form_paragraphs_type_form_builder'; } @@ -162,10 +162,10 @@ function paragraphs_library_form_paragraphs_type_form_alter(&$form, FormStateInt * @see primer_base_form_paragraphs_type_form_alter() */ function paragraphs_library_form_paragraphs_type_form_builder($entity_type, ParagraphsTypeInterface $type, &$form, FormStateInterface $form_state) { - if ($form_state->getValue('disallow_library_conversion')) { - $type->setThirdPartySetting('paragraphs_library', 'disallow_library_conversion', $form_state->getValue('disallow_library_conversion')); + if ($form_state->getValue('allow_library_conversion')) { + $type->setThirdPartySetting('paragraphs_library', 'allow_library_conversion', $form_state->getValue('allow_library_conversion')); } else { - $type->unsetThirdPartySetting('paragraphs_library', 'disallow_library_conversion'); + $type->unsetThirdPartySetting('paragraphs_library', 'allow_library_conversion'); } } diff --git a/modules/paragraphs_library/src/Entity/LibraryItem.php b/modules/paragraphs_library/src/Entity/LibraryItem.php index 9c9d122..768a0d2 100644 --- a/modules/paragraphs_library/src/Entity/LibraryItem.php +++ b/modules/paragraphs_library/src/Entity/LibraryItem.php @@ -123,9 +123,7 @@ class LibraryItem extends ContentEntityBase implements LibraryItemInterface { * {@inheritdoc} */ public static function createFromParagraph(ParagraphInterface $paragraph) { - $disallow_library_conversion = $paragraph->get('type')->entity->getThirdPartySetting('paragraphs_library', 'disallow_library_conversion'); - - if ($disallow_library_conversion) { + if (!$paragraph->getParagraphType()->getThirdPartySetting('paragraphs_library', 'allow_library_conversion', FALSE)) { throw new \Exception(sprintf('%s cannot be converted to library item per configuration', $paragraph->bundle())); } diff --git a/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php b/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php index 46549ee..8f24718 100644 --- a/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php +++ b/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php @@ -171,8 +171,12 @@ class ParagraphsLibraryItemTest extends ParagraphsExperimentalTestBase { 'administer paragraphs library', 'administer paragraphs types' ]); + $edit = ['allow_library_conversion' => 1]; + $this->drupalPostForm('admin/structure/paragraphs_type/text', $edit, 'Save'); + $this->drupalGet('node/add/paragraphed_test'); $this->drupalPostForm(NULL, NULL, 'Add Text'); + $this->assertRaw('Add to library'); $edit = [ 'field_paragraphs[0][subform][paragraphs_text][0][value]' => 'Random text for testing converting into library.', ]; @@ -182,6 +186,7 @@ class ParagraphsLibraryItemTest extends ParagraphsExperimentalTestBase { $edit = [ 'title[0][value]' => 'TextParagraphs', ]; + $this->assertNoRaw('Add to library'); $this->drupalPostForm(NULL, $edit, 'Save and publish'); $this->drupalGet('node/1'); $this->assertText('Random text for testing converting into library.'); @@ -202,11 +207,17 @@ class ParagraphsLibraryItemTest extends ParagraphsExperimentalTestBase { $this->assertText('Random text for testing converting into library.'); // Test disallow convesrion. - $this->drupalGet('admin/structure/paragraphs_type/text'); - $this->drupalPostForm(NULL, ['disallow_library_conversion' => 1], 'Save'); + $edit = ['allow_library_conversion' => FALSE]; + $this->drupalPostForm('admin/structure/paragraphs_type/text', $edit, 'Save'); + + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = \Drupal::service('config.factory'); + $third_party = $config_factory->get('paragraphs.paragraphs_type.text')->get('third_party_settings'); + $this->assertFalse(isset($third_party['paragraphs_library']['allow_library_conversion'])); + $this->drupalGet('node/add/paragraphed_test'); $this->drupalPostForm(NULL, NULL, 'Add Text'); - $this->assertNoText('Add to library'); + $this->assertNoRaw('Add to library'); } /**