You are here

class BootstrapDropdown in Express 8

Pre-processes variables for the "bootstrap_dropdown" theme hook.

Plugin annotation

@BootstrapPreprocess("bootstrap_dropdown");

Hierarchy

Expanded class hierarchy of BootstrapDropdown

File

themes/contrib/bootstrap/src/Plugin/Preprocess/BootstrapDropdown.php, line 24
Contains \Drupal\bootstrap\Plugin\Preprocess\BootstrapDropdown.

Namespace

Drupal\bootstrap\Plugin\Preprocess
View source
class BootstrapDropdown extends PreprocessBase implements PreprocessInterface {

  /**
   * {@inheritdoc}
   */
  protected function preprocessVariables(Variables $variables) {
    $this
      ->preprocessLinks($variables);
    $toggle = Element::create($variables->toggle);
    $toggle
      ->setProperty('split', $variables->split);

    // Convert the items into a proper item list.
    $variables->items = [
      '#theme' => 'item_list__dropdown',
      '#items' => $variables->items,
      '#context' => [
        'alignment' => $variables->alignment,
      ],
    ];

    // Ensure all attributes are proper objects.
    $this
      ->preprocessAttributes();
  }

  /**
   * Preprocess links in the variables array to convert them from dropbuttons.
   *
   * @param \Drupal\bootstrap\Utility\Variables $variables
   *   A variables object.
   */
  protected function preprocessLinks(Variables $variables) {

    // Convert "dropbutton" theme suggestion variables.
    if (Unicode::strpos($variables->theme_hook_original, 'links__dropbutton') !== FALSE && !empty($variables->links)) {
      $operations = !!Unicode::strpos($variables->theme_hook_original, 'operations');

      // Normal dropbutton links are not actually render arrays, convert them.
      foreach ($variables->links as &$element) {
        if (isset($element['title']) && $element['url']) {

          // Preserve query parameters (if any)
          if (!empty($element['query'])) {
            $url_query = $element['url']
              ->getOption('query') ?: [];
            $element['url']
              ->setOption('query', NestedArray::mergeDeep($url_query, $element['query']));
          }

          // Build render array.
          $element = [
            '#type' => 'link',
            '#title' => $element['title'],
            '#url' => $element['url'],
          ];
        }
      }
      $items = Element::createStandalone();
      $primary_action = NULL;
      $links = Element::create($variables->links);

      // Iterate over all provided "links". The array may be associative, so
      // this cannot rely on the key to be numeric, it must be tracked manually.
      $i = -1;
      foreach ($links
        ->children(TRUE) as $key => $child) {
        $i++;

        // The first item is always the "primary link".
        if ($i === 0) {

          // Must generate an ID for this child because the toggle will use it.
          $child
            ->getProperty('id', $child
            ->getAttribute('id', Html::getUniqueId('dropdown-item')));
          $primary_action = $child
            ->addClass('hidden');
        }

        // If actually a "link", add it to the items array directly.
        if ($child
          ->isType('link')) {
          $items->{$key}->link = $child
            ->getArrayCopy();
        }
        else {

          // Hide the original element
          $items->{$key}->element = $child
            ->addClass('hidden')
            ->getArrayCopy();

          // Retrieve any set HTML identifier for the link, generating a new
          // one if necessary.
          $id = $child
            ->getProperty('id', $child
            ->getAttribute('id', Html::getUniqueId('dropdown-item')));
          $items->{$key}->link = Element::createStandalone([
            '#type' => 'link',
            '#title' => $child
              ->getProperty('value', $child
              ->getProperty('title', $child
              ->getProperty('text'))),
            '#url' => Url::fromUserInput('#'),
            '#attributes' => [
              'data-dropdown-target' => "#{$id}",
            ],
          ]);

          // Also hide the real link if it's the primary action.
          if ($i === 0) {
            $items->{$key}->link
              ->addClass('hidden');
          }
        }
      }

      // Create a toggle button, extracting relevant info from primary action.
      $toggle = Element::createStandalone([
        '#type' => 'button',
        '#attributes' => $primary_action
          ->getAttributes()
          ->getArrayCopy(),
        '#value' => $primary_action
          ->getProperty('value', $primary_action
          ->getProperty('title', $primary_action
          ->getProperty('text'))),
      ]);

      // Remove the "hidden" class that was added to the primary action.
      $toggle
        ->removeClass('hidden')
        ->removeAttribute('id')
        ->setAttribute('data-dropdown-target', '#' . $primary_action
        ->getAttribute('id'));

      // Make operations smaller.
      if ($operations) {
        $toggle
          ->setButtonSize('btn-xs', FALSE);
      }

      // Add the toggle render array to the variables.
      $variables->toggle = $toggle
        ->getArrayCopy();

      // Determine if toggle should be a split button.
      $variables->split = count($items) > 1;

      // Add the items variable for "bootstrap_dropdown".
      $variables->items = $items
        ->getArrayCopy();

      // Remove the unnecessary "links" variable now.
      unset($variables->links);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BootstrapDropdown::preprocessLinks protected function Preprocess links in the variables array to convert them from dropbuttons.
BootstrapDropdown::preprocessVariables protected function Preprocess the variables array. Overrides PreprocessBase::preprocessVariables
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$theme protected property The currently set theme object.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocess public function Preprocess theme hook variables. Overrides PreprocessInterface::preprocess
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessElement protected function Preprocess the variables array if an element is present. 8
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.