You are here

function template_preprocess_paragraphs_add_dialog__categorized in Paragraphs Editor Enhancements 8

Prepare variables used in paragraphs-add-dialog--categorized.html.twig.

File

./paragraphs_ee.module, line 278
Main functions for "Paragraphs Editor Enhancements" module.

Code

function template_preprocess_paragraphs_add_dialog__categorized(&$vars) {

  // Define variables for the template.
  $vars += [
    'buttons' => [],
  ];
  $vars['add_mode'] = isset($vars['element']['#add_mode']) ? $vars['element']['#add_mode'] : 'modal';
  if (isset($vars['element']['add_modal_form_area'])) {
    $vars['add'] = $vars['element']['add_modal_form_area'];
  }

  /** @var \Drupal\paragraphs_ee\ParagraphsCategoryInterface[] $categories */
  $paragraphs_categories = \Drupal::entityTypeManager()
    ->getStorage('paragraphs_category')
    ->loadMultiple();

  // Sort the entities using the entity class's sort() method.
  // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
  uasort($paragraphs_categories, [
    ParagraphsCategory::class,
    'sort',
  ]);
  $category_names = array_keys($paragraphs_categories);
  $grouped = array_combine($category_names, array_fill(0, count($category_names), []));
  foreach ($paragraphs_categories as $key => $category) {
    $conditional_id = isset($vars['element']['#id']) ? $vars['element']['#id'] . '-category-' . $key : 'category-' . $key;
    $vars['categories'][$key] = [
      'title' => $category
        ->label(),
      'description' => $category
        ->getDescription(),
      'id' => Html::getUniqueId($conditional_id),
    ];
  }
  $vars['categories']['_none'] = [
    'title' => t('Uncategorized'),
    'description' => '',
  ];

  // Add category for uncategorized items.
  $grouped['_none'] = [];
  foreach (Element::children($vars['element']) as $element_key) {
    if (empty($vars['element'][$element_key]['#paragraphs_category'])) {
      continue;
    }

    // Add button to category.
    $grouped[$vars['element'][$element_key]['#paragraphs_category']][] = $vars['element'][$element_key];
  }
  $vars['groups'] = array_filter($grouped);
  $placeholder_value = 'Paragraphs';
  if (isset($vars['element']['#attributes']['data-widget-title-plural'])) {
    $placeholder_value = $vars['element']['#attributes']['data-widget-title-plural'];
  }
  $vars['filter_placeholder'] = t('Find @placeholder_value', [
    '@placeholder_value' => $placeholder_value,
  ], [
    'context' => 'Paragraphs Editor Enhancements',
  ]);
}