You are here

paragraphs_sets.module in Paragraphs Sets 8

Same filename and directory in other branches
  1. 8.2 paragraphs_sets.module

Main functions of paragraphs_sets.module.

File

paragraphs_sets.module
View source
<?php

/**
 * @file
 * Main functions of paragraphs_sets.module.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\paragraphs_sets\Entity\ParagraphsSet;

/**
 * Implements hook_theme().
 */
function paragraphs_sets_theme() {
  return [
    'field_multiple_value_form__paragraphs_sets' => [
      'render element' => 'element',
      'path' => \drupal_get_path('module', 'paragraphs_sets') . '/templates',
    ],
    'paragraphs_sets_add_dialog' => [
      'render element' => 'element',
      'path' => \drupal_get_path('module', 'paragraphs_sets') . '/templates',
    ],
  ];
}

/**
 * Overrides variables used in field-multiple-value-form.html.twig for sets.
 *
 * @see template_preprocess_field_multiple_value_form()
 */
function paragraphs_sets_preprocess_field_multiple_value_form__paragraphs_sets(&$variables) {
  $element = $variables['element'];
  $variables['multiple'] = $element['#cardinality_multiple'];
  if ($variables['multiple']) {
    $table_id = Html::getUniqueId($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-delta-order';
    $header_attributes = new Attribute([
      'class' => [
        'label',
      ],
    ]);
    if (!empty($element['#required'])) {
      $header_attributes['class'][] = 'js-form-required';
      $header_attributes['class'][] = 'form-required';
    }
    $header = [
      [
        'data' => [
          '#prefix' => '<h4' . $header_attributes . '>',
          '#markup' => $element['#title'],
          '#suffix' => '</h4>',
        ],
        'colspan' => 2,
        'class' => [
          'field-label',
        ],
      ],
      t('Order', [], [
        'context' => 'Sort order',
      ]),
    ];
    $rows = [];

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation).
    $items = [];
    $variables['button'] = [];
    $variables['selection'] = [];
    foreach (Element::children($element) as $key) {
      if ($key === 'add_more') {
        $variables['button'] =& $element[$key];
      }
      elseif ($key === 'set_selection') {
        $variables['selection'] =& $element[$key];
      }
      else {
        $items[] =& $element[$key];
      }
    }
    usort($items, '_field_multiple_value_form_sort_helper');

    // Add the items as table rows.
    foreach ($items as $item) {
      $item['_weight']['#attributes']['class'] = [
        $order_class,
      ];
      $item['#attributes']['data-paragraphs-bundle'] = $item['#paragraphs_bundle'];

      // Remove weight form element from item render array so it can be rendered
      // in a separate table column.
      $delta_element = $item['_weight'];
      unset($item['_weight']);
      $cells = [
        [
          'data' => '',
          'class' => [
            'field-multiple-drag',
          ],
        ],
        [
          'data' => $item,
        ],
        [
          'data' => $delta_element,
          'class' => [
            'delta-order',
          ],
        ],
      ];
      $rows[] = [
        'data' => $cells,
        'class' => [
          'draggable',
          'paragraphs-item',
          Html::getClass("paragraphs-item--{$item['#paragraphs_bundle']}"),
        ],
      ];
    }
    $variables['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [
        'id' => $table_id,
        'class' => [
          'field-multiple-table',
        ],
      ],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => $order_class,
        ],
      ],
    ];
    if (!empty($element['#description'])) {
      $description_id = $element['#attributes']['aria-describedby'];
      $description_attributes['id'] = $description_id;
      $variables['description']['attributes'] = new Attribute($description_attributes);
      $variables['description']['content'] = $element['#description'];

      // Add the description's id to the table aria attributes.
      $variables['table']['#attributes']['aria-describedby'] = $element['#attributes']['aria-describedby'];
    }
  }
  else {
    $variables['elements'] = [];
    $variables['selection'] = [];
    foreach (Element::children($element) as $key) {
      if ($key === 'set_selection') {
        $variables['selection'] =& $element[$key];
      }
      else {
        $variables['elements'][] = $element[$key];
      }
    }
  }

  // Call paragraphs_preprocess_field_multiple_value_form() to fix table header.
  call_user_func_array('paragraphs_preprocess_field_multiple_value_form', [
    &$variables,
  ]);
}

/**
 * Prepares variables for modal form add widget template.
 *
 * Default template: paragraphs-sets-add-dialog.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - buttons: An array of buttons to display in the modal form.
 */
function template_preprocess_paragraphs_sets_add_dialog(array &$variables) {

  // Define variables for the template.
  $variables += [
    'buttons' => [],
    'buttons_title' => $variables['element']['#buttons_title'],
    'sets' => [],
    'sets_title' => t('Paragraph Sets'),
  ];
  foreach (Element::children($variables['element']) as $key) {
    if ($key == 'add_modal_form_area') {

      // $add variable for the add button.
      $variables['add'] = $variables['element'][$key];
    }
    elseif (strpos($key, 'append_selection_button_') === 0) {

      // Buttons for the paragraph sets in the modal form.
      $variables['sets'][$key] = $variables['element'][$key];
    }
    else {

      // Buttons for the paragraph types in the modal form.
      $variables['buttons'][$key] = $variables['element'][$key];
    }
  }
}

/**
 * Helper function to load a paragraphs set.
 *
 * @param string $name
 *   Name (ID) of paragraphs set.
 *
 * @return \Drupal\paragraphs_sets\Entity\ParagraphsSet
 *   The loaded set or NULL if no set with the given name exists.
 */
function paragraphs_set_load($name) {
  return ParagraphsSet::load($name);
}

Functions

Namesort descending Description
paragraphs_sets_preprocess_field_multiple_value_form__paragraphs_sets Overrides variables used in field-multiple-value-form.html.twig for sets.
paragraphs_sets_theme Implements hook_theme().
paragraphs_set_load Helper function to load a paragraphs set.
template_preprocess_paragraphs_sets_add_dialog Prepares variables for modal form add widget template.