You are here

function geysir_preprocess_node in Geysir 8

Implements hook_preprocess_HOOK().

Using hook_preprocess_node().

File

./geysir.module, line 441
Geysir module file.

Code

function geysir_preprocess_node(&$vars) {

  /** @var \Drupal\node\Entity\Node $node */
  $node = $vars["node"];
  if (empty($node) || !$node
    ->isLatestRevision() || !$node
    ->access('update') || !Drupal::currentUser()
    ->hasPermission('geysir manage paragraphs from front-end') || !$node
    ->isDefaultTranslation()) {
    return;
  }
  $field_definitions = $node
    ->getFieldDefinitions();

  // Check if multiple paragraph fields.
  $paragraph_fields = [];
  foreach ($field_definitions as $field_definition) {

    /** @var \Drupal\Core\Field\BaseFieldDefinition $field_Definition */
    if ($field_definition
      ->getType() == 'entity_reference_revisions') {

      /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage_definition */
      $field_storage_definition = $field_definition
        ->getFieldStorageDefinition();
      if ($field_storage_definition
        ->getSetting('target_type') == 'paragraph') {
        $paragraph_fields[$field_storage_definition
          ->get('field_name')] = $field_definition
          ->getLabel();
      }
    }
  }
  if (empty($paragraph_fields)) {
    return;
  }
  foreach ($paragraph_fields as $field_name => $field_label) {

    // Check if the paragraph field already has paragraphs added.
    if (isset($vars['content'][$field_name]) && empty($vars['content'][$field_name]['#items'])) {
      $field_wrapper_id = Html::getUniqueId('geysir--' . $field_name);
      $markup = geysir_get_add_first_paragraph_markup($node, $field_name, $field_wrapper_id, $field_label);
      $markup = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'data-geysir-field-paragraph-field-wrapper' => $field_wrapper_id,
        ],
        '#value' => $markup,
      ];
      $vars['content'][$field_name]['#suffix'] = Drupal::service('renderer')
        ->render($markup);
    }
  }
}