PHP Method drupal\core\render\ Element::setAttributes Code Examples

PHP drupal\core\render\Element::setAttributes - 22 examples found. These are the top rated real world PHP examples of method drupal\core\render\Element::setAttributes extracted from open source projects. You can rate examples to help us improve the quality of examples
Frequently Used Methods
Example #1
0
 /**
  * Prepares a #type 'color' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderColor($element)
 {
     $element['#attributes']['type'] = 'color';
     Element::setAttributes($element, array('id', 'name', 'value'));
     static::setAttributes($element, array('form-color'));
     return $element;
 }
 /**
  * Prepares a #type 'password' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #size, #maxlength,
  *   #placeholder, #required, #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderPassword($element)
 {
     $element['#attributes']['type'] = 'password';
     Element::setAttributes($element, array('id', 'name', 'size', 'maxlength', 'placeholder'));
     static::setAttributes($element, array('form-text'));
     return $element;
 }
Example #3
0
 /**
  * Prepares a #type 'range' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #min, #max, #attributes,
  *   #step.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderRange($element)
 {
     $element['#attributes']['type'] = 'range';
     Element::setAttributes($element, array('id', 'name', 'value', 'step', 'min', 'max'));
     static::setAttributes($element, array('form-range'));
     return $element;
 }
Example #4
0
 /**
  * Prepares a #type 'file' render element for input.html.twig.
  *
  * For assistance with handling the uploaded file correctly, see the API
  * provided by file.inc.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #name, #size, #description, #required,
  *   #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderFile($element)
 {
     $element['#attributes']['type'] = 'file';
     Element::setAttributes($element, array('id', 'name', 'size'));
     static::setAttributes($element, array('js-form-file', 'form-file'));
     return $element;
 }
 /**
  * Prepares a #type 'number' render element for theme_input().
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #min, #max, #placeholder,
  *   #required, #attributes, #step, #size.
  *
  * @return array
  *   The $element with prepared variables ready for theme_input().
  */
 public static function preRenderNumber($element)
 {
     $element['#attributes']['type'] = 'number';
     Element::setAttributes($element, array('id', 'name', 'value', 'step', 'min', 'max', 'placeholder', 'size'));
     static::setAttributes($element, array('form-number'));
     return $element;
 }
Example #6
0
 /**
  * Prepares a #type 'url' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #size, #maxlength,
  *   #placeholder, #required, #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderUrl($element)
 {
     $element['#attributes']['type'] = 'url';
     Element::setAttributes($element, array('id', 'name', 'value', 'size', 'maxlength', 'placeholder'));
     static::setAttributes($element, array('form-url'));
     return $element;
 }
Example #7
0
 /**
  * Prepares a #type 'uc_quantity' render element for theme_input().
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #description, #size, #maxlength,
  *   #placeholder, #min, #max, #step, #required, #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for theme_input().
  */
 public static function preRenderQuantity($element)
 {
     $element['#attributes']['type'] = 'number';
     $element['#attributes']['min'] = 0;
     $element['#attributes']['step'] = 1;
     Element::setAttributes($element, array('id', 'name', 'value', 'size', 'maxlength', 'placeholder', 'min', 'max', 'step'));
     static::setAttributes($element, array('form-uc-quantity'));
     return $element;
 }
Example #8
0
 /**
  * Adds form-specific attributes to a 'date' #type element.
  *
  * Supports HTML5 types of 'date', 'datetime', 'datetime-local', and 'time'.
  * Falls back to a plain textfield with JS datepicker support. Used as a
  * sub-element by the datetime element type.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #options, #description, #required,
  *   #attributes, #id, #name, #type, #min, #max, #step, #value, #size. The
  *   #name property will be sanitized before output. This is currently done by
  *   initializing Drupal\Core\Template\Attribute with all the attributes.
  *
  * @return array
  *   The $element with prepared variables ready for #theme 'input__date'.
  */
 public static function preRenderDate($element)
 {
     if (empty($element['#attributes']['type'])) {
         $element['#attributes']['type'] = 'date';
     }
     Element::setAttributes($element, array('id', 'name', 'type', 'min', 'max', 'step', 'value', 'size'));
     static::setAttributes($element, array('form-' . $element['#attributes']['type']));
     return $element;
 }
Example #9
0
 /**
  * Prepares a #type 'radio' render element for theme_input().
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #required, #return_value, #value, #attributes, #title,
  *   #description.
  *
  * Note: The input "name" attribute needs to be sanitized before output, which
  *       is currently done by initializing Drupal\Core\Template\Attribute with
  *       all the attributes.
  *
  * @return array
  *   The $element with prepared variables ready for theme_input().
  */
 public static function preRenderRadio($element)
 {
     $element['#attributes']['type'] = 'radio';
     Element::setAttributes($element, array('id', 'name', '#return_value' => 'value'));
     if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
         $element['#attributes']['checked'] = 'checked';
     }
     static::setAttributes($element, array('form-radio'));
     return $element;
 }
Example #10
0
 /**
  * Prepares a #type 'checkbox' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #title, #value, #return_value, #description, #required,
  *   #attributes, #checked.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderCheckbox($element)
 {
     $element['#attributes']['type'] = 'checkbox';
     Element::setAttributes($element, array('id', 'name', '#return_value' => 'value'));
     // Unchecked checkbox has #value of integer 0.
     if (!empty($element['#checked'])) {
         $element['#attributes']['checked'] = 'checked';
     }
     static::setAttributes($element, array('form-checkbox'));
     return $element;
 }
/**
 * Pre-processes variables for the "bootstrap_panel" theme hook.
 *
 * See template for list of available variables.
 *
 * @see bootstrap-panel.html.twig
 *
 * @ingroup theme_preprocess
 */
function bootstrap_preprocess_bootstrap_panel(&$variables)
{
    $element = $variables['element'];
    Element::setAttributes($element, array('id'));
    Element\RenderElement::setAttributes($element);
    $variables['attributes'] = $element['#attributes'];
    $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL;
    $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL;
    $variables['title_display'] = isset($element['#title_display']) ? $element['#title_display'] : NULL;
    $variables['children'] = $element['#children'];
    $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
    $variables['legend']['title'] = !empty($element['#title']) ? Xss::filterAdmin($element['#title']) : '';
    $variables['legend']['attributes'] = new Attribute();
    $variables['legend_span']['attributes'] = new Attribute();
    if (!empty($element['#description'])) {
        $description_id = $element['#attributes']['id'] . '--description';
        $description_attributes['id'] = $description_id;
        $variables['description']['attributes'] = new Attribute($description_attributes);
        $variables['description']['content'] = $element['#description'];
        // Add the description's id to the fieldset aria attributes.
        $variables['attributes']['aria-describedby'] = $description_id;
    }
    $variables['collapsible'] = FALSE;
    if (isset($element['#collapsible'])) {
        $variables['collapsible'] = $element['#collapsible'];
        $variables['attributes']['class'][] = 'collapsible';
    }
    $variables['collapsed'] = FALSE;
    if (isset($element['#collapsed'])) {
        $variables['collapsed'] = $element['#collapsed'];
    }
    // Force grouped fieldsets to not be collapsible (for vertical tabs).
    if (!empty($element['#group'])) {
        $variables['collapsible'] = FALSE;
        $variables['collapsed'] = FALSE;
    }
    if (!isset($element['#id']) && $variables['collapsible']) {
        $element['#id'] = \Drupal\Component\Utility\Html::getUniqueId('bootstrap-panel');
    }
    $variables['target'] = NULL;
    if (isset($element['#id'])) {
        if (!isset($variables['attributes']['id'])) {
            $variables['attributes']['id'] = $element['#id'];
        }
        $variables['target'] = '#' . $element['#id'] . ' > .collapse';
    }
    // Iterate over optional variables.
    $keys = array('description', 'prefix', 'suffix', 'title', 'value');
    foreach ($keys as $key) {
        $variables[$key] = !empty($element["#{$key}"]) ? $element["#{$key}"] : FALSE;
    }
}
Example #12
0
 /**
  * Prepares a #type 'button' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #attributes, #button_type, #name, #value.
  *
  * The #button_type property accepts any value, though core themes have CSS that
  * styles the following button_types appropriately: 'primary', 'danger'.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderButton($element)
 {
     $element['#attributes']['type'] = 'submit';
     Element::setAttributes($element, array('id', 'name', 'value'));
     $element['#attributes']['class'][] = 'button';
     if (!empty($element['#button_type'])) {
         $element['#attributes']['class'][] = 'button--' . $element['#button_type'];
     }
     $element['#attributes']['class'][] = 'js-form-submit';
     $element['#attributes']['class'][] = 'form-submit';
     if (!empty($element['#attributes']['disabled'])) {
         $element['#attributes']['class'][] = 'is-disabled';
     }
     return $element;
 }
Example #13
0
 /**
  * Prepares a #type 'button' render element for theme_input().
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #attributes, #button_type, #name, #value.
  *
  * The #button_type property accepts any value, though core themes have CSS that
  * styles the following button_types appropriately: 'primary', 'danger'.
  *
  * @return array
  *   The $element with prepared variables ready for theme_input().
  */
 public static function preRenderButton($element)
 {
     $element['#attributes']['type'] = 'submit';
     Element::setAttributes($element, array('id', 'name', 'value'));
     $element['#attributes']['class'][] = 'button';
     if (!empty($element['#button_type'])) {
         $element['#attributes']['class'][] = 'button--' . $element['#button_type'];
     }
     // @todo Various JavaScript depends on this button class.
     $element['#attributes']['class'][] = 'form-submit';
     if (!empty($element['#attributes']['disabled'])) {
         $element['#attributes']['class'][] = 'is-disabled';
     }
     return $element;
 }
Example #14
0
/**
 * Preprocess input.
 */
function bootstrap_preprocess_input(&$variables)
{
    $element =& $variables['element'];
    $attributes = new Attribute($variables['attributes']);
    // Set the element's attributes.
    \Drupal\Core\Render\Element::setAttributes($element, array('id', 'name', 'value', 'type'));
    // Handle button inputs.
    if (_bootstrap_is_button($element)) {
        $variables['attributes']['class'][] = 'btn';
        _bootstrap_colorize_button($variables);
        _bootstrap_iconize_button($element);
        // Add button size, if necessary.
        if ($size = bootstrap_setting('button_size')) {
            $variables['attributes']['class'][] = $size;
        }
        // Add in the button type class.
        $variables['attributes']['class'][] = 'form-' . $element['#type'];
        $variables['label'] = $element['#value'];
    }
    _bootstrap_prerender_input($variables);
    // Autocomplete fields.
    if (!empty($element['#autocomplete_route_name']) && Drupal::PathValidator($element['#autocomplete_route_name'])) {
        $variables['autocomplete'] = TRUE;
        // Attributes for hidden input field.
        $autocomplete_attributes = new Attribute();
        $autocomplete_attributes['type'] = 'hidden';
        $autocomplete_attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
        $autocomplete_attributes['value'] = Drupal::Url($element['#autocomplete_route_name'], $element['#autocomplete_route_parameters']);
        $autocomplete_attributes['disabled'] = 'disabled';
        $autocomplete_attributes['class'] = 'autocomplete';
        // Uses icon for autocomplete "throbber".
        $icon = _bootstrap_icon('refresh');
        // Fallback to using core's throbber.
        if (empty($icon)) {
            $icon = array('#type' => 'container', '#attributes' => array('class' => array('ajax-progress', 'ajax-progress-throbber', 'invisible')), 'throbber' => array('#type' => 'html_tag', '#tag' => 'div', '#attributes' => array('class' => array('throbber'))));
        }
        $variables['autocomplete_icon'] = $icon;
        $variables['autocomplete_attributes'] = $autocomplete_attributes;
    }
    // Search fields.
    if ($element['#type'] == 'search') {
        $attributes['placeholder'] = t('Search');
        $attributes['data-original-title'] = t('Enter the terms you wish to search for.');
    }
    // Additional Twig variables.
    $variables['icon'] = $element['#icon'];
    $variables['element'] = $element;
}
Example #15
0
 /**
  * Adds form element theming to details.
  *
  * @param $element
  *   An associative array containing the properties and children of the
  *   details.
  *
  * @return
  *   The modified element.
  */
 public static function preRenderDetails($element)
 {
     Element::setAttributes($element, array('id'));
     // The .js-form-wrapper class is required for #states to treat details like
     // containers.
     static::setAttributes($element, array('js-form-wrapper', 'form-wrapper'));
     // Collapsible details.
     $element['#attached']['library'][] = 'core/drupal.collapse';
     if (!empty($element['#open'])) {
         $element['#attributes']['open'] = 'open';
     }
     // Do not render optional details elements if there are no children.
     if (isset($element['#parents'])) {
         $group = implode('][', $element['#parents']);
         if (!empty($element['#optional']) && !Element::getVisibleChildren($element['#groups'][$group])) {
             $element['#printed'] = TRUE;
         }
     }
     return $element;
 }
Example #16
0
 /**
  * {@inheritdoc}
  */
 public static function preRenderButton($element)
 {
     $element['#attributes']['type'] = 'image';
     Element::setAttributes($element, array('id', 'name', 'value'));
     $element['#attributes']['src'] = file_create_url($element['#src']);
     if (!empty($element['#title'])) {
         $element['#attributes']['alt'] = $element['#title'];
         $element['#attributes']['title'] = $element['#title'];
     }
     $element['#attributes']['class'][] = 'image-button';
     if (!empty($element['#button_type'])) {
         $element['#attributes']['class'][] = 'image-button--' . $element['#button_type'];
     }
     $element['#attributes']['class'][] = 'js-form-submit';
     $element['#attributes']['class'][] = 'form-submit';
     if (!empty($element['#attributes']['disabled'])) {
         $element['#attributes']['class'][] = 'is-disabled';
     }
     return $element;
 }
Example #17
0
 /**
  * Maps an element's properties to its attributes array.
  *
  * @param array $map
  *   An associative array whose keys are element property names and whose
  *   values are the HTML attribute names to set on the corresponding
  *   property; e.g., array('#propertyname' => 'attributename'). If both names
  *   are identical except for the leading '#', then an attribute name value is
  *   sufficient and no property name needs to be specified.
  *
  * @return $this
  */
 public function map(array $map)
 {
     \Drupal\Core\Render\Element::setAttributes($this->array, $map);
     return $this;
 }
Example #18
0
 /**
  * Tests the setAttributes() method.
  *
  * @dataProvider providerTestSetAttributes
  */
 public function testSetAttributes($element, $map, $expected_element)
 {
     Element::setAttributes($element, $map);
     $this->assertSame($expected_element, $element);
 }
Example #19
0
 /**
  * Prepares a select render element.
  */
 public static function preRenderSelect($element)
 {
     Element::setAttributes($element, array('id', 'name', 'size'));
     static::setAttributes($element, array('form-select'));
     return $element;
 }
Example #20
0
 /**
  * Prepares a #type 'hidden' render element for input.html.twig.
  *
  * @param array $element
  *   An associative array containing the properties of the element.
  *   Properties used: #name, #value, #attributes.
  *
  * @return array
  *   The $element with prepared variables ready for input.html.twig.
  */
 public static function preRenderHidden($element)
 {
     $element['#attributes']['type'] = 'hidden';
     Element::setAttributes($element, array('name', 'value'));
     return $element;
 }
Example #21
0
  /**
   * Prepares a #type 'linkit' render element for input.html.twig.
   *
   * @param array $element
   *   An associative array containing the properties of the element.
   *   Properties used: #title, #value, #description, #size, #attributes.
   *
   * @return array
   *   The $element with prepared variables ready for input.html.twig.
   */
  public static function preRenderLinkitElement($element) {
    $element['#attributes']['type'] = 'text';
    Element::setAttributes($element, array('id', 'name', 'value', 'size'));
    static::setAttributes($element, array('form-text'));

    return $element;
  }
Example #22
-1
 /**
  * #pre_render callback to transform children of an element into #rows suitable for theme_table().
  *
  * This function converts sub-elements of an element of #type 'table' to be
  * suitable for theme_table():
  * - The first level of sub-elements are table rows. Only the #attributes
  *   property is taken into account.
  * - The second level of sub-elements is converted into columns for the
  *   corresponding first-level table row.
  *
  * Simple example usage:
  * @code
  * $form['table'] = array(
  *   '#type' => 'table',
  *   '#header' => array(t('Title'), array('data' => t('Operations'), 'colspan' => '1')),
  *   // Optionally, to add tableDrag support:
  *   '#tabledrag' => array(
  *     array(
  *       'action' => 'order',
  *       'relationship' => 'sibling',
  *       'group' => 'thing-weight',
  *     ),
  *   ),
  * );
  * foreach ($things as $row => $thing) {
  *   $form['table'][$row]['#weight'] = $thing['weight'];
  *
  *   $form['table'][$row]['title'] = array(
  *     '#type' => 'textfield',
  *     '#default_value' => $thing['title'],
  *   );
  *
  *   // Optionally, to add tableDrag support:
  *   $form['table'][$row]['#attributes']['class'][] = 'draggable';
  *   $form['table'][$row]['weight'] = array(
  *     '#type' => 'textfield',
  *     '#title' => t('Weight for @title', array('@title' => $thing['title'])),
  *     '#title_display' => 'invisible',
  *     '#size' => 4,
  *     '#default_value' => $thing['weight'],
  *     '#attributes' => array('class' => array('thing-weight')),
  *   );
  *
  *   // The amount of link columns should be identical to the 'colspan'
  *   // attribute in #header above.
  *   $form['table'][$row]['edit'] = array(
  *     '#type' => 'link',
  *     '#title' => t('Edit'),
  *     '#url' => Url::fromRoute('entity.test_entity.edit_form', ['test_entity' => $row]),
  *   );
  * }
  * @endcode
  *
  * @param array $element
  *   A structured array containing two sub-levels of elements. Properties used:
  *   - #tabledrag: The value is a list of $options arrays that are passed to
  *     drupal_attach_tabledrag(). The HTML ID of the table is added to each
  *     $options array.
  *
  * @return array
  *
  * @see theme_table()
  * @see drupal_process_attached()
  * @see drupal_attach_tabledrag()
  */
 public static function preRenderTable($element)
 {
     foreach (Element::children($element) as $first) {
         $row = array('data' => array());
         // Apply attributes of first-level elements as table row attributes.
         if (isset($element[$first]['#attributes'])) {
             $row += $element[$first]['#attributes'];
         }
         // Turn second-level elements into table row columns.
         // @todo Do not render a cell for children of #type 'value'.
         // @see http://drupal.org/node/1248940
         foreach (Element::children($element[$first]) as $second) {
             // Assign the element by reference, so any potential changes to the
             // original element are taken over.
             $column = array('data' => &$element[$first][$second]);
             // Apply wrapper attributes of second-level elements as table cell
             // attributes.
             if (isset($element[$first][$second]['#wrapper_attributes'])) {
                 $column += $element[$first][$second]['#wrapper_attributes'];
             }
             $row['data'][] = $column;
         }
         $element['#rows'][] = $row;
     }
     // Take over $element['#id'] as HTML ID attribute, if not already set.
     Element::setAttributes($element, array('id'));
     // Add sticky headers, if applicable.
     if (count($element['#header']) && $element['#sticky']) {
         $element['#attached']['library'][] = 'core/drupal.tableheader';
         // Add 'sticky-enabled' class to the table to identify it for JS.
         // This is needed to target tables constructed by this function.
         $element['#attributes']['class'][] = 'sticky-enabled';
     }
     // If the table has headers and it should react responsively to columns hidden
     // with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
     // and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
     if (count($element['#header']) && $element['#responsive']) {
         $element['#attached']['library'][] = 'core/drupal.tableresponsive';
         // Add 'responsive-enabled' class to the table to identify it for JS.
         // This is needed to target tables constructed by this function.
         $element['#attributes']['class'][] = 'responsive-enabled';
     }
     // If the custom #tabledrag is set and there is a HTML ID, add the table's
     // HTML ID to the options and attach the behavior.
     if (!empty($element['#tabledrag']) && isset($element['#attributes']['id'])) {
         foreach ($element['#tabledrag'] as $options) {
             $options['table_id'] = $element['#attributes']['id'];
             drupal_attach_tabledrag($element, $options);
         }
     }
     return $element;
 }