/** * {@inheritdoc} */ public function preprocessElement(Variables $variables, $hook, array $info) { // Set errors flag. $variables['errors'] = $variables->element->hasProperty('has_error'); if ($variables->element->getProperty('autocomplete_route_name')) { $variables['is_autocomplete'] = TRUE; } // See http://getmaterialize.com/css/#forms-controls. $checkbox = $variables['is_checkbox'] = $variables->element->isType('checkbox'); $radio = $variables['is_radio'] = $variables->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'] = $variables->element->getProperty('form_group', !$variables['is_checkbox'] && !$variables['is_radio'] && !$variables->element->isType(['hidden', 'textarea'])); // Add label_display and label variables to template. $display = $variables['label_display'] = $variables['title_display'] = $variables->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 ($variables->element->hasProperty('label_attributes')) { $label->setAttributes($variables->element->getProperty('label_attributes')); } } // Remove the #field_prefix and #field_suffix values set in // template_preprocess_form_element(). These are handled on the input level. // @see \Drupal\materialize\Plugin\Preprocess\Input::preprocess(). if ($variables->element->hasProperty('input_group') || $variables->element->hasProperty('input_group_button')) { $variables['prefix'] = FALSE; $variables['suffix'] = FALSE; } }
/** * Pre-render render array element callback. * * @param array $element * The render array element. * * @return array * The modified render array element. */ public static function preRender(array $element) { if (!empty($element['#materialize_ignore_pre_render'])) { return $element; } $e = Element::create($element); if ($e->isType('machine_name')) { $e->addClass('form-inline', 'wrapper_attributes'); } // Add smart descriptions to the element, if necessary. $e->smartDescription(); return $element; }
/** * {@inheritdoc} */ public function preprocessVariables(Variables $variables, $hook, array $info) { if ($variables->theme_hook_original === 'links' && $variables->hasClass('operations')) { $variables->addClass('list-inline'); foreach ($variables->links as &$data) { $link = Element::create($data['link']); $link->addClass(['btn', 'btn-sm']); $link->colorize(); $link->setIcon(); if ($this->theme->getSetting('tooltip_enabled')) { $link->setAttribute('data-toggle', 'tooltip'); $link->setAttribute('data-placement', 'bottom'); } } } $this->preprocessAttributes($variables, $hook, $info); }
/** * {@inheritdoc} */ public function preprocessVariables(Variables $variables, $hook, array $info) { $options = []; $file = $variables['file'] instanceof File ? $variables['file'] : File::load($variables['file']->fid); $url = file_create_url($file->getFileUri()); $file_size = $file->getSize(); $mime_type = $file->getMimeType(); // Set options as per anchor format described at // http://microformats.org/wiki/file-format-examples $options['attributes']['type'] = "{$mime_type}; length={$file_size}"; // Use the description as the link text if available. if (empty($variables['description'])) { $link_text = $file->getFilename(); } else { $link_text = $variables['description']; $options['attributes']['title'] = $file->getFilename(); } // Retrieve the generic mime type from core (mislabeled as "icon_class"). $generic_mime_type = file_icon_class($mime_type); // Map the generic mime types to an icon and state. $mime_map = ['application-x-executable' => ['label' => t('binary file'), 'icon' => 'console'], 'audio' => ['label' => t('audio file'), 'icon' => 'headphones'], 'image' => ['label' => t('image'), 'icon' => 'picture'], 'package-x-generic' => ['label' => t('archive'), 'icon' => 'compressed'], 'text' => ['label' => t('document'), 'icon' => 'file'], 'video' => ['label' => t('video'), 'icon' => 'film']]; // Retrieve the mime map array. $mime = isset($mime_map[$generic_mime_type]) ? $mime_map[$generic_mime_type] : ['label' => t('file'), 'icon' => 'file', 'state' => 'primary']; // Classes to add to the file field for icons. // $variables->addClass([ // 'file', // // Add a specific class for each and every mime type. // 'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']), // // Add a more general class for groups of well known mime types. // 'file--' . $generic_mime_type, // ]); // Set the icon for the mime type. $icon = Materialize::material_icons_font($mime['icon']); $variables->icon = Element::create($icon)->addClass('text-primary')->getArray(); $options['attributes']['title'] = t('Open @mime in new window', ['@mime' => $mime['label']]); if ($this->theme->getSetting('tooltip_enabled')) { $options['attributes']['data-toggle'] = 'tooltip'; $options['attributes']['data-placement'] = 'bottom'; } $variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUri($url, $options)); // Add the file size as a variable. $variables->file_size = format_size($file_size); // Preprocess attributes. $this->preprocessAttributes($variables, $hook, $info); }
/** * Sets up the vertical tab groupings. * * @param array $form * Nested array of form elements that comprise the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function createGroups(array &$form, FormStateInterface $form_state) { $f = Element::create($form, $form_state); // Vertical tabs for global settings provided by core or contrib modules. if (!isset($form['global'])) { $form['global'] = ['#type' => 'vertical_tabs', '#weight' => -9, '#prefix' => '<h2><small>' . t('Override Global Settings') . '</small></h2>']; } // Iterate over existing children and move appropriate ones to global group. foreach ($f->children() as $child) { if ($child->isType(['details', 'fieldset']) && !$child->hasProperty('group')) { $child->setProperty('type', 'details'); $child->setProperty('group', 'global'); } } // Provide the necessary default groups. $form['materialize'] = ['#type' => 'vertical_tabs', '#attached' => ['library' => ['materialize/theme-settings']], '#prefix' => '<h2><small>' . t('Materialize Settings') . '</small></h2>', '#weight' => -10]; $groups = ['general' => t('General'), 'components' => t('Components'), 'javascript' => t('JavaScript'), 'advanced' => t('Advanced')]; foreach ($groups as $group => $title) { $form[$group] = ['#type' => 'details', '#title' => $title, '#group' => 'materialize']; } }
/** * Traverses an element to find the closest button. * * @param \Drupal\materialize\Utility\Element $element * The element to iterate over. * * @return \Drupal\materialize\Utility\Element|FALSE * The first button element or FALSE if no button could be found. */ protected static function &findButton(Element $element) { $button = FALSE; foreach ($element->children() as $child) { if ($child->isButton()) { $button = $child; } if ($result =& self::findButton($child)) { $button = $result; } } return $button; }
/** * Prepends a property with a value. * * @param string $name * The name of the property to set. * @param mixed $value * The value of the property to set. * * @return $this */ public function prependProperty($name, $value) { $property =& $this->getProperty($name); $value = $value instanceof Element ? $value->getArray() : $value; // If property isn't set, just set it. if (!isset($property)) { $property = $value; return $this; } if (is_array($property)) { array_unshift($property, Element::create($value)->getArray()); } else { $property = (string) $value . (string) $property; } return $this; }