You are here

function template_preprocess_micon_icon_list in Micon 2.x

Same name and namespace in other branches
  1. 8 micon.theme.inc \template_preprocess_micon_icon_list()

Prepares variables for the Micon icon list template.

Default template: micon-icon-list.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing the icon
  • attributes: HTML attributes for the containing element.

File

./micon.theme.inc, line 156
Contains micon.theme.inc.

Code

function template_preprocess_micon_icon_list(array &$variables) {
  $variables['micon'] = $micon = $variables['element']['#micon'];
  $variables['icons'] = [];
  $variables['content']['filters'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'table-filter',
        'js-show',
      ],
    ],
  ];
  $variables['content']['filters']['text'] = [
    '#type' => 'search',
    '#title' => t('Filter icons'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => t('Filter by name or description'),
    '#description' => t('Enter a part of the icon name.'),
    '#description_display' => 'after',
    '#attributes' => [
      'class' => [
        'micon-filter-text',
      ],
      'data-list' => '.micon-icon-list',
      'autocomplete' => 'off',
    ],
  ];
  foreach ($micon
    ->getIcons() as $key => $icon) {
    $variables['icons'][$key]['preview'] = $icon
      ->toRenderable();
    $variables['icons'][$key]['selector'] = [
      '#type' => 'textfield',
      '#value' => $icon
        ->getSelector(),
      '#attributes' => [
        'class' => [
          'selector',
        ],
      ],
    ];
    $variables['icons'][$key]['search']['#markup'] = '<div class="micon-filter-text visually-hidden" data-tags="' . $icon
      ->getTags() . '">' . $icon
      ->getName() . '</div>';
    if ($icon
      ->getType() == 'font') {

      // Show hex for CSS implementation.
      $variables['icons'][$key]['hex'] = [
        '#type' => 'textfield',
        '#value' => $icon
          ->getHex(),
        '#attributes' => [
          'class' => [
            'info',
          ],
        ],
      ];
    }
    if ($icon
      ->getType() == 'image') {

      // Show renderable markup.
      $render = $variables['icons'][$key]['preview'];
      $rendered = \Drupal::service('renderer')
        ->render($render)
        ->__toString();
      $rendered = preg_replace('/<!--(.|\\s)*?-->/', '', $rendered);
      $variables['icons'][$key]['markup'] = [
        '#type' => 'textfield',
        '#value' => $rendered,
        '#attributes' => [
          'class' => [
            'info',
          ],
        ],
      ];
    }
  }
}