bef_entity_select_buttons-1.0.0-alpha1/bef_entity_select_buttons.module

bef_entity_select_buttons.module
<?php

use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;

/**
 * Implements hook_theme().
 */
function bef_entity_select_buttons_theme(): array {
  return [
    'bef_entity_select_buttons' => [
      'render element' => 'element',
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * HOOK: bef_entity_select_buttons
 */
function bef_entity_select_buttons_preprocess_bef_entity_select_buttons(&$variables): void {
  // Call the preprocess hook of the default bef links.
  call_user_func_array('template_preprocess_bef_links', [&$variables]);

  $variables['#attached']['library'][] = 'bef_entity_select_buttons/general';

  $contentPathsService = $variables['element']['#entity_type']
    ? Drupal::service('bef_entity_select_buttons.content_paths')
    : null;

  foreach (Element::children($variables['element']) as $option) {
    $element = &$variables['element'][$option];

    array_push(
      $element['#attributes']['class'],
      'bef-entity-select-buttons__option-button',
      'button',
      ($variables['element']['#button_small'] ?? false) ? 'button--small' : null,
      // @todo Semantically incorrect, do we have no 'selected' or 'active' class for buttons?
      $option === $variables['element']['#value'] ? 'button--primary' : null,
    );

    $element['#title'] = Markup::create(
      // Wrap the title in span for styling purposes.
      "<span>{$variables['element'][$option]['#title']}</span>"
    );

    // Add a link to create entity
    if($contentPathsService) {
      $url = $contentPathsService->getAddContentUrl($variables['element']['#entity_type'], $option);
      if($url) {
        $url->setOptions([
          'attributes' => [
            'class' => [
              'button',
              'bef-entity-select-buttons__add-entity-button',
              ($variables['element']['#button_small'] ?? false) ? 'button--small' : null,
            ],
          ],
        ]);
        $element['#suffix'] = Link::fromTextAndUrl('', $url)->toString();
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc