How to alter Summary

Created on 8 July 2020, over 4 years ago
Updated 15 March 2023, almost 2 years 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 over 1 year ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 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 over 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 over 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 over 1 year ago
    160 pass, 14 fail
  • Status changed to Needs work 5 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.

  • Status changed to RTBC 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States scotwith1t Birmingham, AL

    Super helpful stuff, thanks all! +1 and setting to RTBC unless there's some reason it should not be committed...?

  • πŸ‡«πŸ‡·France luukyb

    We're using this too. Would love to see this committed. Thanks!

Production build 0.71.5 2024