Add the paragraph to summary preprocess variables

Created on 8 July 2020, almost 4 years ago
Updated 24 October 2023, 8 months ago

Original issue summary

How can I alter the Summary of a Paragraph?

I need to change this field value but I couldn't find a way to do it.

Is there a hook?

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇪🇸Spain UriDrupal

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇱🇻Latvia Phonoman

    This issue is clearly about altering the Paragraph Summary - aka, the closed "preview" of field values when editing some entity with paragraphs.
    #2 is the UI description when checking the paragraph list, not related.
    And altering this still seems like a big question mark - there seems to not be a straightforward way of doing this as you would, for example, with preprocessing variables.
    There seems to be a preprocess summary method, which you can technically use to change the summary... However, it only contains the text elements and not the entity in question, so you cannot easily append it without fetching the entity from route, then loading/digging however deep you need with tons of if's.

    /**
     * Prepares variables for.
     *
     * Default template: paragraphs-summary.html.twig
     *
     * @param array $variables
     *   An associative array containing:
     *   - buttons: An array of buttons to display in the modal form.
     */
    function template_preprocess_paragraphs_summary(&$variables) {
      $variables['content'] = $variables['element']['#summary']['content'];
      $variables['behaviors'] = $variables['element']['#summary']['behaviors'];
      $variables['expanded'] = !empty($variables['element']['#expanded']);
    }
  • 🇦🇺Australia sime Canberra

    Yes, I'm wondering why the paragraphs entity is not attached to the variables that reach the preprocess and twig template.

  • 🇦🇺Australia sime Canberra

    I propose that in ParagraphsSummaryFormatter->viewElements(), the entity, or the entity ID/Revision is attached to $elements['#attached'].

  • 🇦🇺Australia sime Canberra

    Speculative to try it out...

    /**
     * Implements TEMPLATE_preprocess_paragraphs_summary
     */
    function mymodule_preprocess_paragraphs_summary(&$variables) {
       var_dump($variables['element']['#paragraph']);
    }
    
  • 🇦🇺Australia sime Canberra

    Using the module paragraph_view_mode.

    /**
     * Implements TEMPLATE_preprocess_paragraphs_summary
     */
    function MYMODULE_preprocess_paragraphs_summary(&$variables) {
      // Requires https://www.drupal.org/project/paragraphs/issues/3157819#comment-15149289
      $paragraph = $variables['element']['#paragraph'];
      $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('paragraph', $paragraph->getType());
      $view = $paragraph->get('paragraph_view_mode')->value;
      $variables['content'] = array_merge(['as ' . $view_modes[$view]], $variables['content']);
    }
    
  • Status changed to Needs review 8 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 8 months ago
    152 pass, 24 fail
  • 🇪🇸Spain interdruper

    Patch for 8.x-1.16 with the approach suggested by @sime in #9 is provided.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    152 pass, 24 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 8 months ago
    160 pass, 14 fail
  • 🇪🇸Spain interdruper

    Summary renderization was not covered in all cases in the patch #11. New patch attached.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    160 pass, 14 fail
Production build 0.69.0 2024