You are here

public function Paragraph::getSummaryItems in Paragraphs 8

Returns the summary items of the Paragraph.

Parameters

array $options: (optional) Array of additional options, with the following elements:

  • 'show_behavior_summary': Whether the summary should contain the behavior settings. Defaults to TRUE to show behavior settings in the summary.
  • 'depth_limit': Depth limit of how many nested paragraph summaries are allowed. Defaults to 1 to show nested paragraphs only on top level.

Return value

array A list of summary items, grouped into the keys 'content' and 'behaviors'.

Overrides ParagraphInterface::getSummaryItems

1 call to Paragraph::getSummaryItems()
Paragraph::getSummary in src/Entity/Paragraph.php
Returns a short summary for the Paragraph.

File

src/Entity/Paragraph.php, line 451

Class

Paragraph
Defines the Paragraph entity.

Namespace

Drupal\paragraphs\Entity

Code

public function getSummaryItems(array $options = []) {
  $summary = [
    'content' => [],
    'behaviors' => [],
  ];
  $show_behavior_summary = isset($options['show_behavior_summary']) ? $options['show_behavior_summary'] : TRUE;
  $depth_limit = isset($options['depth_limit']) ? $options['depth_limit'] : 1;

  // Add content summary items.
  $this->summaryCount = 0;
  $components = \Drupal::service('entity_display.repository')
    ->getFormDisplay('paragraph', $this
    ->getType())
    ->getComponents();
  uasort($components, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  foreach (array_keys($components) as $field_name) {

    // Components can be extra fields, check if the field really exists.
    if (!$this
      ->hasField($field_name)) {
      continue;
    }
    $field_definition = $this
      ->getFieldDefinition($field_name);

    // We do not add content to the summary from base fields, skip them
    // keeps performance while building the paragraph summary.
    if (!$field_definition instanceof FieldConfigInterface || !$this
      ->get($field_name)
      ->access('view')) {
      continue;
    }
    if ($field_definition
      ->getType() == 'image' || $field_definition
      ->getType() == 'file') {
      $file_summary = $this
        ->getFileSummary($field_name);
      if ($file_summary != '') {
        $summary['content'][] = $file_summary;
      }
    }
    $text_summary = $this
      ->getTextSummary($field_name, $field_definition);
    if ($text_summary != '') {
      $summary['content'][] = $text_summary;
    }
    if ($field_definition
      ->getType() == 'entity_reference_revisions') {

      // Decrease the depth, since we are entering a nested paragraph.
      $nested_summary = $this
        ->getNestedSummary($field_name, [
        'show_behavior_summary' => FALSE,
        'depth_limit' => $depth_limit - 1,
      ]);
      $summary['content'] = array_merge($summary['content'], $nested_summary);
    }
    if ($field_definition
      ->getType() === 'entity_reference') {
      $referenced_entities = $this
        ->get($field_name)
        ->referencedEntities();

      /** @var \Drupal\Core\Entity\EntityInterface[] $referenced_entities */
      foreach ($referenced_entities as $referenced_entity) {
        if ($referenced_entity
          ->access('view label')) {

          // Switch to the entity translation in the current context.
          $entity = \Drupal::service('entity.repository')
            ->getTranslationFromContext($referenced_entity, $this->activeLangcode);
          $summary['content'][] = $entity
            ->label();
        }
      }
    }

    // Add the Block admin label referenced by block_field.
    if ($field_definition
      ->getType() == 'block_field') {
      if (!empty($this
        ->get($field_name)
        ->first())) {
        if ($block = $block_admin_label = $this
          ->get($field_name)
          ->first()
          ->getBlock()) {
          $block_admin_label = $block
            ->getPluginDefinition()['admin_label'];
        }
        $summary['content'][] = $block_admin_label;
      }
    }
    if ($field_definition
      ->getType() == 'link') {
      if (!empty($this
        ->get($field_name)
        ->first())) {

        // If title is not set, fallback to the uri.
        if ($title = $this
          ->get($field_name)->title) {
          $summary['content'][] = $title;
        }
        else {
          $summary['content'][] = $this
            ->get($field_name)->uri;
        }
      }
    }
  }

  // Add behaviors summary items.
  if ($show_behavior_summary) {
    $paragraphs_type = $this
      ->getParagraphType();
    foreach ($paragraphs_type
      ->getEnabledBehaviorPlugins() as $plugin) {
      if ($plugin_summary = $plugin
        ->settingsSummary($this)) {
        foreach ($plugin_summary as $plugin_summary_element) {
          if (!is_array($plugin_summary_element)) {
            $plugin_summary_element = [
              'value' => $plugin_summary_element,
            ];
          }
          $summary['behaviors'][] = $plugin_summary_element;
        }
      }
    }
  }
  return $summary;
}