Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function preprocessVariables(Variables $variables, $hook, array $info)
 {
     // Retrieve the ID, generating one if needed.
     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-panel')));
     unset($variables['id']);
     // Handle collapsible state.
     if ($variables['heading'] && $variables['collapsible']) {
         // Retrieve the body ID attribute.
         if ($body_id = $variables->getAttribute('id', "{$id}--content", 'body_attributes')) {
             // Ensure the target is set.
             if ($variables['target'] = $variables->offsetGet('target', "#{$body_id}")) {
                 // Set additional necessary attributes to the heading.
                 $variables->setAttributes(['aria-controls' => preg_replace('/^#/', '', $variables['target']), 'aria-expanded' => !$variables['collapsed'] ? 'true' : 'false', 'aria-pressed' => !$variables['collapsed'] ? 'true' : 'false', 'data-toggle' => 'collapse', 'role' => 'button'], 'heading_attributes');
             }
         }
     }
     // Ensure there is a valid panel state.
     if (!$variables->offsetGet('panel_type')) {
         $variables->offsetSet('panel_type', 'default');
     }
     // Convert the description variable.
     $this->preprocessDescription($variables, $hook, $info);
     // Ensure all attributes are proper objects.
     $this->preprocessAttributes($variables, $hook, $info);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     $variables->addClass(['image-widget', 'js-form-managed-file', 'form-managed-file', 'clearfix']);
     $data =& $variables->offsetGet('data', []);
     foreach ($element->children() as $key => $child) {
         // Modify the label to be a placeholder instead.
         if ($key === 'alt') {
             $child->setProperty('form_group', FALSE);
             $placeholder = (string) $child->getAttribute('placeholder');
             if (!$placeholder) {
                 $label = ['#theme' => 'form_element_label'];
                 $label += array_intersect_key($child->getArray(), array_flip(['#id', '#required', '#title', '#title_display']));
                 $child->setProperty('title_display', 'invisible');
                 $placeholder = trim(strip_tags(Element::create($label)->render()));
                 if ($child->getProperty('required')) {
                     $child->setProperty('description', t('@description (Required)', ['@description' => $child->getProperty('description')]));
                 }
             }
             if ($placeholder) {
                 $child->setAttribute('placeholder', $placeholder);
             }
         }
         $data[$key] = $child->getArray();
     }
 }
Example #3
0
 /**
  * Converts any set description variable into a traversable array.
  *
  * @param \Drupal\bootstrap\Utility\Variables $variables
  *   A variables object.
  * @param string $hook
  *   The name of the theme hook.
  * @param array $info
  *   The theme hook info array.
  *
  * @see https://www.drupal.org/node/2324025
  */
 protected function preprocessDescription(Variables $variables, $hook, array $info)
 {
     if ($variables->offsetGet('description')) {
         // Retrieve the description attributes.
         $description_attributes = $variables->offsetGet('description_attributes', []);
         // Remove standalone description attributes.
         $variables->offsetUnset('description_attributes');
         // Build the description attributes.
         if ($id = $variables->getAttribute('id')) {
             $variables->setAttribute('aria-describedby', "{$id}--description");
             $description_attributes['id'] = "{$id}--description";
         }
         // Replace the description variable.
         $variables->offsetSet('description', ['attributes' => new Attribute($description_attributes), 'content' => $variables['description'], 'position' => $variables->offsetGet('description_display', 'after')]);
     }
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 protected function preprocessVariables(Variables $variables, $hook, array $info)
 {
     // Retrieve the ID, generating one if needed.
     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-carousel')));
     unset($variables['id']);
     // Build slides.
     foreach ($variables->slides as $key => &$slide) {
         if (!isset($slide['attributes'])) {
             $slide['attributes'] = [];
         }
         $slide['attributes'] = new Attribute($slide['attributes']);
     }
     // Build controls.
     if ($variables->controls) {
         $left_icon = Bootstrap::glyphicon('chevron-left');
         $right_icon = Bootstrap::glyphicon('chevron-right');
         $url = Url::fromUserInput("#{$id}");
         $variables->controls = ['left' => ['#type' => 'link', '#title' => new FormattableMarkup(Element::create($left_icon)->render() . '<span class="sr-only">@text</span>', ['@text' => t('Previous')]), '#url' => $url, '#attributes' => ['class' => ['left', 'carousel-control'], 'role' => 'button', 'data-slide' => 'prev']], 'right' => ['#type' => 'link', '#title' => new FormattableMarkup(Element::create($right_icon)->render() . '<span class="sr-only">@text</span>', ['@text' => t('Next')]), '#url' => $url, '#attributes' => ['class' => ['right', 'carousel-control'], 'role' => 'button', 'data-slide' => 'next']]];
     }
     // Build indicators.
     if ($variables->indicators) {
         $variables->indicators = ['#theme' => 'item_list__bootstrap_carousel_indicators', '#list_type' => 'ol', '#items' => array_keys($variables->slides), '#target' => "#{$id}", '#start_index' => $variables->start_index];
     }
     // Ensure all attributes are proper objects.
     $this->preprocessAttributes($variables, $hook, $info);
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function preprocessVariables(Variables $variables)
 {
     // Ensure a unique ID, generating one if needed.
     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'progress-bar')));
     $variables->setAttribute('id', $id);
     unset($variables['id']);
     // Preprocess attributes.
     $this->preprocessAttributes();
 }
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Variables $variables, $hook, array $info)
 {
     $variables->addClass(['image-widget', 'js-form-managed-file', 'form-managed-file', 'clearfix']);
     /** @var \Drupal\file\Entity\File $file */
     foreach ($variables->element->getProperty('files') as $file) {
         $variables->element->{'file_' . $file->id()}->filename->setProperty('suffix', ' <span class="file-size badge">' . format_size($file->getSize()) . '</span>');
     }
     $data =& $variables->offsetGet('data', []);
     foreach ($variables->element->children() as $key => $child) {
         $data[$key] = $child->getArray();
     }
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 protected function preprocessVariables(Variables $variables, $hook, array $info)
 {
     // Immediately log an error and return if Bootstrap modals are not enabled.
     if (!$this->theme->getSetting('modal_enabled')) {
         \Drupal::logger('bootstrap')->error(t('Bootstrap modals are not enabled.'));
         return;
     }
     // Retrieve the ID, generating one if needed.
     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-modal')));
     $variables->setAttribute('id', $id);
     unset($variables['id']);
     if ($variables->title) {
         $title_id = $variables->getAttribute('id', "{$id}--title", $variables::TITLE);
         $variables->setAttribute('id', $title_id, $variables::TITLE);
         $variables->setAttribute('aria-labelledby', $title_id);
     }
     // Use a provided modal size or retrieve the default theme setting.
     $variables->size = $variables->size ?: $this->theme->getSetting('modal_size');
     // Convert the description variable.
     $this->preprocessDescription($variables, $hook, $info);
     // Ensure all attributes are proper objects.
     $this->preprocessAttributes($variables, $hook, $info);
 }