Follow-up to #2847053: Convert a normal paragraph into the library

Problem/Motivation

A paragraph with its potential deep nested children group is converted once into a library item. Then it is used many times.

Chances are that at some point you want to override a specific placement. You then want to unlink the reference and start with a clone of the original items. People might think about these items as templates and prepare items that are cloned many times with ease: select as reference, convert, override, done! Much easier than create everything and configure all the settings for complex items...

Separate issue: we will add a checkbox to flag the items as a template. Then placing them will always clone. And if fields are empty, templates are proposed immediately in the empty widget. UX Awesomeness!

Proposed resolution

Add an item to the dropbuttons of the library item widget to convert it back to a paragraph. Pressing it would convert the paragraph type of in the widget from a library item reference to a normal paragraph. We don't delete the library item in this process.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Primsi created an issue. See original summary.

thenchev’s picture

Assigned: Unassigned » thenchev
thenchev’s picture

The part about not deleting the library item... Are we keeping the library item with the paragraph/s inside and clone a new paragraph when converting or take them out and leave an empty library item? For me the laving empty makes sense...

thenchev’s picture

Status: Active » Needs review
StatusFileSize
new3.98 KB

I guess we wait until #2862428: Add hook function for widget gets in.

thenchev’s picture

StatusFileSize
new2.48 KB
new16.24 KB

Added some tests.

primsi’s picture

Status: Needs review » Postponed (maintainer needs more info)

Needs reroll. Also let's postpone this until the hook gets in.

thenchev’s picture

StatusFileSize
new6.46 KB

reroll

toncic’s picture

Status: Postponed (maintainer needs more info) » Needs work

After committing #2862428: Add hook function for widget we can continue with this issue.

toncic’s picture

Status: Needs work » Needs review

Just to run tests.

toncic’s picture

Status: Needs review » Needs work

Needs reroll and changed to use alter hook function from paragraphs.

berdir’s picture

Priority: Major » Normal

As discussed, this is IMHO not major.

tbonomelli’s picture

StatusFileSize
new5.8 KB

rerolled patch

berdir’s picture

Assigned: thenchev » Unassigned
Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work
  1. +++ b/modules/paragraphs_library/paragraphs_library.module
    @@ -143,3 +143,93 @@ function _paragraphs_library_count_usage(array $usage_data) {
    + * Implements hook_field_widget_form_alter().
    + */
    +function paragraphs_library_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
    +  if ($context['widget']->getPluginId() == 'paragraphs') {
    +    $parents = $element['#field_parents'];
    +    $field_definition = $context['items']->getFieldDefinition();
    +    $field_name = $field_definition->getName();
    +    $widget_state = WidgetBase::getWidgetState($parents, $field_name, $form_state);
    +
    +    // Convert only library items.
    +    $paragraph = $widget_state['paragraphs'][$context['delta']];
    +    $paragraphs_entity = isset($paragraph) ? $paragraph['entity'] : FALSE;
    +    if ($paragraphs_entity && $paragraphs_entity->bundle() == 'from_library') {
    +      $id_prefix = implode('-', array_merge($parents, [$field_name, $context['delta']]));
    +      $element['top']['links']['library_to_paragraph'] = [
    +        '#type' => 'submit',
    +        '#value' => t('Convert to paragraph'),
    +        '#name' => strtr($id_prefix, '-', '_') . '_remove_library_item',
    

    this also needs to be converted to the new hook that we have now to alter operations on the paragraphs widget.

  2. +++ b/modules/paragraphs_library/paragraphs_library.module
    @@ -143,3 +143,93 @@ function _paragraphs_library_count_usage(array $usage_data) {
    +
    +  // Inserting new element in the array.
    +  $widget_state = WidgetBase::getWidgetState($parents, $field_name, $form_state);
    +  $delta = $button['#delta'];
    +  $widget_state['original_deltas'] = array_merge($widget_state['original_deltas'], ['1' => 1]);
    +
    +  $paragraphs = [];
    +  if ($widget_state['paragraphs'][$delta]['entity']->hasField('field_reusable_paragraph')) {
    +    $library_items = $widget_state['paragraphs'][$delta]['entity']
    +      ->field_reusable_paragraph
    +      ->referencedEntities();
    +
    +    foreach ($library_items as $library_item) {
    +      foreach ($library_item->paragraphs->getValue() as $paragraph) {
    +        $paragraphs[] = $paragraph;
    +      }
    +    }
    +  }
    +
    +  $paragraph = \Drupal::entityTypeManager()
    +    ->getStorage('paragraph')
    +    ->loadRevision($paragraphs[0]['target_revision_id']);
    

    this is too generic and complicated, the reusable paragraph field can only have one value and the library can also only have one value, we do not need to loop or something like that. just access $library_item->get('paragraphs')->entity

    We are also not adding anything new, we just replace one.

miro_dietiker’s picture

Issue tags: +Needs tests

This needs tests.

I guess we always replace one library paragraph with one effective paragraph entity. So we don't have any shifting of id's and weight in all the various states.

mbovan’s picture

Assigned: Unassigned » mbovan
mbovan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new7.3 KB
new9.01 KB

Addressed #14, improvements and test updates.

mbovan’s picture

Most of the test fails from above are related to #2855309: Create ParagraphsTestBaseTrait..

mbovan’s picture

Status: Needs review » Postponed
Related issues: +#2855309: Create ParagraphsTestBaseTrait.

Postponed until #2855309: Create ParagraphsTestBaseTrait. follow-up gets committed.

mbovan’s picture

Status: Postponed » Needs review

#2855309: Create ParagraphsTestBaseTrait. was committed. Reopening this issue.

miro_dietiker’s picture

Issue summary: View changes

Updating summary, this is not a less frequent requirement. In contrast, see above..

ginovski’s picture

Status: Needs review » Needs work

Needs reroll

+++ b/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php
@@ -33,7 +33,7 @@ class ParagraphsLibraryItemTest extends ParagraphsExperimentalTestBase {
+    $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs', 'entity_reference_paragraphs');

This should be tested with the experimental widget I think, so the second and third argument can be removed.


+++ b/modules/paragraphs_library/paragraphs_library.module
@@ -21,17 +21,37 @@ function paragraphs_library_paragraph_widget_dropbutton_alter(&$links, &$context
+      '#access' => $paragraphs_entity->access('update'),

I guess this will removed due to the new third party setting (allow_library_conversion)

Also, had some exceptions in the test about the ajax path

miro_dietiker’s picture

Connecting to template issue.

mbovan’s picture

Status: Needs work » Needs review
StatusFileSize
new7.71 KB

Rerolled, updates, addressed #22.

Checking CI results...

primsi’s picture

Status: Needs review » Needs work
  1. +++ b/modules/paragraphs_library/paragraphs_library.module
    @@ -22,22 +22,39 @@ function paragraphs_library_paragraph_widget_dropbutton_alter(&$links, &$context
    +      '#submit' => ['_paragraphs_library_library_item_convert_submit'],
    

    I think submit functions do not get prefixed by "_".

  2. +++ b/modules/paragraphs_library/src/Tests/ParagraphsLibraryItemTest.php
    @@ -175,6 +175,47 @@ class ParagraphsLibraryItemTest extends ParagraphsExperimentalTestBase {
    +    $this->assertText('Paragraph re usable paragraph label has been created.');
    

    Single word: reusable

Apart from this two nit-picks looks ok. Also tested it manually and everything seems to work fine.

mbovan’s picture

Status: Needs work » Needs review
StatusFileSize
new7.7 KB
new3.61 KB

Applied suggestions from #25.

  • Primsi committed 2e312cb on 8.x-1.x authored by mbovan
    Issue #2858036 by mbovan, Denchev, tbonomelli, Primsi, toncic, Berdir,...
primsi’s picture

Status: Needs review » Fixed

Committed, thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.