You are here

function paragraphs_edit_preprocess_field in Paragraphs Edit 8

Parameters

$variables:

See also

template_preprocess_field()

File

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

Code

function paragraphs_edit_preprocess_field(&$variables) {
  if (empty($variables['field_type']) || $variables['field_type'] !== 'entity_reference_revisions') {
    return;
  }
  $element = $variables['element'];
  if ($element['#entity_type'] !== 'node') {
    return;
  }

  /** @var \Drupal\node\NodeInterface $parent */
  $parent = $element['#object'];

  /** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $field */
  $field = $element['#items'];
  $field_definition = $field
    ->getFieldDefinition();
  $field_storage_definition = $field_definition
    ->getFieldStorageDefinition();
  if ($field_storage_definition
    ->getSetting('target_type') !== 'paragraph') {
    return;
  }
  $delta = 0;
  while (!empty($element[$delta])) {

    /** @var \Drupal\paragraphs\ParagraphInterface $entity */
    $entity = $field
      ->get($delta)->entity;
    BubbleableMetadata::createFromObject($parent)
      ->applyTo($variables['items'][$delta]['content']);
    $variables['items'][$delta]['content']['#contextual_links']['paragraph'] = [
      'route_parameters' => [
        'node' => $parent
          ->id(),
        'field' => $variables['field_name'],
        'delta' => $delta,
        'paragraph' => $entity
          ->id(),
      ],
      'metadata' => [
        'changed' => $parent
          ->getChangedTime(),
      ],
    ];
    $delta++;
  }
}