You are here

function paragraphs_edit_paragraph_view_alter in Paragraphs Edit 8.2

Implements hook_ENTITY_TYPE_view_alter() for paragraph entities.

File

./paragraphs_edit.module, line 29
Allows users to edit, clone and delete paragraphs.

Code

function paragraphs_edit_paragraph_view_alter(array &$build, ParagraphInterface $entity, EntityViewDisplayInterface $display) {

  // Skip adding contextual links if the Paragraph is in a QuickEdit form
  // context to avoid causing conflicts with Quickedit JS.
  if (\Drupal::routeMatch()
    ->getRouteName() == 'quickedit.field_form') {
    return;
  }

  // Skip adding contextual links, if the Paragraph is in a AdminRoute context.
  // That can happen on forms with Paragraph form display setting
  // "Edit mode: Preview" enabled.
  if (\Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    return;
  }
  $root_parent = \Drupal::service('paragraphs_edit.lineage.inspector')
    ->getRootParent($entity);
  if ($root_parent) {
    $build['#contextual_links']['paragraph'] = [
      'route_parameters' => [
        'root_parent_type' => $root_parent
          ->getEntityTypeId(),
        'root_parent' => $root_parent
          ->id(),
        'paragraph' => $entity
          ->id(),
      ],
    ];
    if ($root_parent instanceof EntityChangedInterface) {
      $build['#contextual_links']['paragraph']['metadata'] = [
        'changed' => $root_parent
          ->getChangedTime(),
      ];
    }
  }
}