How to alter Summary

Created on 8 July 2020, over 4 years ago
Updated 15 March 2023, over 1 year ago

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?

💬 Support request
Status

Active

Version

1.0

Component

Documentation

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 Melbourne

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

  • 🇦🇺Australia sime Melbourne

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

  • 🇦🇺Australia sime Melbourne

    Speculative to try it out...

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

    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 about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year 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 about 1 year 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 about 1 year 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 about 1 year ago
    160 pass, 14 fail
  • Status changed to Needs work 2 months ago
  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    Thanks for the patch comment 13 (and also the work done previously in the issue too!).

    Impossible to customize properly paragraphs summary without that.

Production build 0.71.5 2024