Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     // Set errors flag.
     $variables['errors'] = $element->hasProperty('has_error');
     if ($element->getProperty('autocomplete_route_name')) {
         $variables['is_autocomplete'] = TRUE;
     }
     // See http://getbootstrap.com/css/#forms-controls.
     $checkbox = $variables['is_checkbox'] = $element->isType('checkbox');
     $radio = $variables['is_radio'] = $element->isType('radio');
     // Determine if the form element should have the "form-group" class added.
     // Use an explicitly set property from the element or use its other
     // properties as the criteria to determine if it should be set.
     $variables['is_form_group'] = $element->getProperty('form_group', !$variables['is_checkbox'] && !$variables['is_radio'] && !$element->isType(['hidden', 'textarea']));
     // Add label_display and label variables to template.
     $display = $variables['label_display'] = $variables['title_display'] = $element->getProperty('title_display');
     // Place single checkboxes and radios in the label field.
     if (($checkbox || $radio) && $display !== 'none' && $display !== 'invisible') {
         $label = Element::create($variables['label']);
         $children =& $label->getProperty('children', '');
         $children .= $variables['children'];
         unset($variables['children']);
         // Pass the label attributes to the label, if available.
         if ($element->hasProperty('label_attributes')) {
             $label->setAttributes($element->getProperty('label_attributes'));
         }
     }
     // Remove the #field_prefix and #field_suffix values set in
     // template_preprocess_form_element(). These are handled at the input level.
     // @see \Drupal\bootstrap\Plugin\Preprocess\Input::preprocess().
     unset($variables['prefix']);
     unset($variables['suffix']);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public static function preRenderElement(Element $element)
 {
     // Injects the icon into the title (the only way this is possible).
     if ($icon =& $element->getProperty('icon')) {
         $title = $element->getProperty('title');
         // Handle #icon_only.
         if ($element->getProperty('icon_only')) {
             if ($attribute_title = $element->getAttribute('title', '')) {
                 $title .= ' - ' . $attribute_title;
             }
             $element->setAttribute('title', $title)->addClass('icon-only')->setProperty('title', $icon);
             if (Bootstrap::getTheme()->getSetting('tooltip_enabled')) {
                 $element->setAttribute('data-toggle', 'tooltip');
             }
             return;
         }
         // Handle #icon_position.
         $position = $element->getProperty('icon_position', 'before');
         // Render #icon and trim it (so it doesn't add underlines in whitespace).
         $rendered_icon = trim(Element::create($icon)->render());
         // Default position is before.
         $markup = "{$rendered_icon}@title";
         if ($position === 'after') {
             $markup = "@title{$rendered_icon}";
         }
         // Replace the title and set an icon position class.
         $element->setProperty('title', new FormattableMarkup($markup, ['@title' => $title]))->addClass("icon-{$position}");
     }
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public static function processElement(Element $element, FormStateInterface $form_state, array &$complete_form)
 {
     $element->setProperty('title_display', 'invisible');
     $element->setAttribute('placeholder', $element->getProperty('placeholder', $element->getProperty('title', t('Search'))));
     if (!$element->hasProperty('description')) {
         $element->setProperty('description', t('Enter the terms you wish to search for.'));
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public static function preRenderElement(Element $element)
 {
     $element['#attached']['library'][] = 'bootstrap/dropdown';
     // Enable targeted theming of specific dropbuttons (e.g., 'operations' or
     // 'operations__node').
     if ($subtype = $element->getProperty('subtype')) {
         $element->setProperty('theme', $element->getProperty('theme') . "__{$subtype}");
     }
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     // Create variables for #input_group and #input_group_button flags.
     $variables['input_group'] = $element->getProperty('input_group') || $element->getProperty('input_group_button');
     // Map the element properties.
     $variables->map(['attributes' => 'attributes', 'field_prefix' => 'prefix', 'field_suffix' => 'suffix']);
     // Ensure attributes are proper objects.
     $this->preprocessAttributes();
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     $element->colorize();
     $element->setButtonSize();
     $element->setIcon($element->getProperty('icon'));
     $variables['icon_only'] = $element->getProperty('icon_only');
     $variables['label'] = $element->getProperty('value');
     if ($element->getProperty('split')) {
         $variables->map([$variables::SPLIT_BUTTON]);
     }
     parent::preprocessElement($element, $variables);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     $element->map(['id', 'name', 'value', 'type']);
     // Autocomplete.
     if ($route = $element->getProperty('autocomplete_route_name')) {
         $variables['autocomplete'] = TRUE;
     }
     // Create variables for #input_group and #input_group_button flags.
     $variables['input_group'] = $element->getProperty('input_group') || $element->getProperty('input_group_button');
     // Map the element properties.
     $variables->map(['attributes' => 'attributes', 'icon' => 'icon', 'field_prefix' => 'prefix', 'field_suffix' => 'suffix', 'type' => 'type']);
     // Ensure attributes are proper objects.
     $this->preprocessAttributes();
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     $link = $element->getProperty('link');
     $link += ['localized_options' => []];
     $link['localized_options']['set_active_class'] = TRUE;
     $icon = Bootstrap::glyphiconFromString($link['title']);
     $options = isset($link['localized_options']) ? $link['localized_options'] : [];
     if (isset($link['url'])) {
         // Turn link into a mini-button and colorize based on title.
         $class = Bootstrap::cssClassFromString($link['title'], 'default');
         if (!isset($options['attributes']['class'])) {
             $options['attributes']['class'] = [];
         }
         $string = is_string($options['attributes']['class']);
         if ($string) {
             $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
         }
         $options['attributes']['class'][] = 'btn';
         $options['attributes']['class'][] = 'btn-xs';
         $options['attributes']['class'][] = 'btn-' . $class;
         if ($string) {
             $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
         }
         $variables['link'] = ['#type' => 'link', '#title' => SafeMarkup::format(\Drupal::service('renderer')->render($icon) . '@text', ['@text' => $link['title']]), '#options' => $options, '#url' => $link['url']];
     } else {
         $variables['link'] = ['#type' => 'link', '#title' => $link['title'], '#options' => $options, '#url' => $link['url']];
     }
 }
 /**
  * {@inheritdoc}
  */
 public function preprocessElement(Element $element, Variables $variables)
 {
     // Wrap header columns in label element for Bootstrap.
     if ($variables['multiple']) {
         $header = [['data' => ['#prefix' => '<label class="label">', 'title' => ['#markup' => $element->getProperty('title')], '#suffix' => '</label>'], 'colspan' => 2, 'class' => ['field-label', !empty($element['#required']) ? 'form-required' : '']], t('Order', [], ['context' => 'Sort order'])];
         $variables['table']['#header'] = $header;
     }
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public static function processElement(Element $element, FormStateInterface $form_state, array &$complete_form)
 {
     $ajax_wrapper_id = $element->upload_button->getProperty('ajax')['wrapper'];
     if ($prefix = $element->getProperty('prefix')) {
         $prefix = preg_replace('/<div id="' . $ajax_wrapper_id . '">/', '<div id="' . $ajax_wrapper_id . '" class="form-group">', $prefix);
         $element->setProperty('prefix', $prefix);
     }
 }
Esempio n. 11
0
 /**
  * Maps an element's properties to the variables attributes array.
  *
  * @param array $map
  *   An associative array whose keys are element property names and whose
  *   values are the variable names to set in the variables array; e.g.,
  *   array('#property_name' => 'variable_name'). If both names are identical
  *   except for the leading '#', then an attribute name value is sufficient
  *   and no property name needs to be specified.
  * @param bool $overwrite
  *   If the variable exists, it will be overwritten. This does not apply to
  *   attribute arrays, they will always be merged recursively.
  *
  * @return $this
  */
 public function map(array $map, $overwrite = TRUE)
 {
     // Immediately return if there is no element in the variable array.
     if (!$this->element) {
         return $this;
     }
     // Iterate over each map item.
     foreach ($map as $property => $variable) {
         // If the key is numeric, the attribute name needs to be taken over.
         if (is_int($property)) {
             $property = $variable;
         }
         // Merge attributes from the element.
         if (strpos($property, 'attributes') !== FALSE) {
             $this->setAttributes($this->element->getAttributes($property)->getArrayCopy(), $variable);
         } elseif ($overwrite || !$this->offsetExists($variable)) {
             $this->offsetSet($variable, $this->element->getProperty($property));
         }
     }
     return $this;
 }
Esempio n. 12
0
 /**
  * Processes elements that have input groups.
  *
  * @param \Drupal\bootstrap\Utility\Element $element
  *   The element object.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param array $complete_form
  *   The complete form structure.
  */
 protected static function processInputGroups(Element $element, FormStateInterface $form_state, array &$complete_form)
 {
     // Automatically inject the nearest button found after this element if
     // #input_group_button exists.
     if ($element->getProperty('input_group_button')) {
         // Obtain the parent array to limit search.
         $array_parents = $element->getProperty('array_parents', []);
         // Remove the current element from the array.
         array_pop($array_parents);
         // Retrieve the parent element.
         $parent = Element::create(NestedArray::getValue($complete_form, $array_parents), $form_state);
         // Find the closest button.
         if ($button = self::findButton($parent)) {
             $element->appendProperty('field_suffix', $button->setIcon());
             $button->setProperty('access', FALSE);
         }
     }
     $input_group_attributes = ['class' => ['input-group-' . ($element->getProperty('input_group_button') ? 'btn' : 'addon')]];
     if ($prefix = $element->getProperty('field_prefix')) {
         $element->setProperty('field_prefix', ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => $input_group_attributes, '#value' => Element::create($prefix)->render(), '#weight' => -1]);
     }
     if ($suffix = $element->getProperty('field_suffix')) {
         $element->setProperty('field_suffix', ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => $input_group_attributes, '#value' => Element::create($suffix)->render(), '#weight' => 1]);
     }
 }