class BootstrapDropdown in Express 8
Pre-processes variables for the "bootstrap_dropdown" theme hook.
Plugin annotation
@BootstrapPreprocess("bootstrap_dropdown");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase implements PreprocessInterface
- class \Drupal\bootstrap\Plugin\Preprocess\BootstrapDropdown implements PreprocessInterface
- class \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase implements PreprocessInterface
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\PreprocessView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BootstrapDropdown:: |
protected | function | Preprocess links in the variables array to convert them from dropbuttons. | |
BootstrapDropdown:: |
protected | function |
Preprocess the variables array. Overrides PreprocessBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The currently set theme object. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
PreprocessBase:: |
protected | property | The theme hook invoked. | |
PreprocessBase:: |
protected | property | The theme hook info array from the theme registry. | |
PreprocessBase:: |
protected | property | The Variables object. | |
PreprocessBase:: |
public | function |
Preprocess theme hook variables. Overrides PreprocessInterface:: |
|
PreprocessBase:: |
protected | function | Ensures all attributes have been converted to an Attribute object. | |
PreprocessBase:: |
protected | function | Converts any set description variable into a traversable array. | |
PreprocessBase:: |
protected | function | Preprocess the variables array if an element is present. | 8 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |