Exemplo n.º 1
5
 /**
  * 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;
 }
Exemplo n.º 2
4
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     foreach (Element::children($form_state->getValue('stock')) as $sku) {
         $stock = $form_state->getValue(['stock', $sku]);
         db_merge('uc_product_stock')->key(array('sku' => $sku))->updateFields(array('active' => $stock['active'], 'stock' => $stock['stock'], 'threshold' => $stock['threshold']))->insertFields(array('sku' => $sku, 'active' => $stock['active'], 'stock' => $stock['stock'], 'threshold' => $stock['threshold'], 'nid' => $form_state->getValue('nid')))->execute();
     }
     drupal_set_message($this->t('Stock settings saved.'));
 }
Exemplo n.º 3
3
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = array();
     $settings = $this->getSettings();
     $items_array = array();
     foreach ($items as $item) {
         $items_array[] = $item;
     }
     // Merge defaults from the formatters and ensure proper ordering.
     $this->prepareFormatters($this->fieldDefinition->getType(), $settings['formatters']);
     // Loop through each formatter in order.
     foreach ($settings['formatters'] as $name => $options) {
         // Run any unrendered items through the formatter.
         $formatter_items = array_diff_key($items_array, $element);
         $formatter_instance = $this->getFormatter($options);
         $formatter_instance->prepareView(array($items->getEntity()->id() => $items));
         if ($result = $formatter_instance->viewElements($items, $langcode)) {
             // Only add visible content from the formatter's render array result
             // that matches an unseen delta.
             $visible_deltas = Element::getVisibleChildren($result);
             $visible_deltas = array_intersect($visible_deltas, array_keys($formatter_items));
             $element += array_intersect_key($result, array_flip($visible_deltas));
             // If running this formatter completed the output for all items, then
             // there is no need to loop through the rest of the formatters.
             if (count($element) == count($items_array)) {
                 break;
             }
         }
     }
     // Ensure the resulting elements are ordered properly by delta.
     ksort($element);
     return $element;
 }
Exemplo n.º 4
2
 /**
  * {@inheritdoc}
  */
 public function checkoutSettingsForm(array $form, FormStateInterface $form_state, JobInterface $job)
 {
     if (!Element::children($form)) {
         $form['#description'] = t("The @translator translator doesn't provide any checkout settings.", array('@translator' => $job->getTranslator()->label()));
     }
     return $form;
 }
Exemplo n.º 5
2
 /**
  * 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;
 }
Exemplo n.º 6
2
 /**
  * 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;
 }
 protected function assertFallbackFormatter($entity, array $formatters = array(), array $expected_output)
 {
     $display = array('type' => 'fallback', 'settings' => array('formatters' => $formatters));
     $output = $entity->test_text->view($display);
     $output = array_intersect_key($output, Element::children($output));
     $this->assertEqual($output, $expected_output);
 }
Exemplo n.º 8
2
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $filter_values = $this->translateFilterValues();
     $langcode = $filter_values['langcode'];
     $this->languageManager->reset();
     $languages = $this->languageManager->getLanguages();
     $langname = isset($langcode) ? $languages[$langcode]->getName() : "- None -";
     $form['#attached']['library'][] = 'locale/drupal.locale.admin';
     $form['langcode'] = array('#type' => 'value', '#value' => $filter_values['langcode']);
     $form['strings'] = array('#type' => 'table', '#tree' => TRUE, '#language' => $langname, '#header' => [$this->t('Source string'), $this->t('Translation for @language', ['@language' => $langname])], '#empty' => $this->t('No strings available.'), '#attributes' => ['class' => ['locale-translate-edit-table']]);
     if (isset($langcode)) {
         $strings = $this->translateFilterLoadStrings();
         $plural_formulas = $this->state->get('locale.translation.plurals') ?: array();
         foreach ($strings as $string) {
             // Cast into source string, will do for our purposes.
             $source = new SourceString($string);
             // Split source to work with plural values.
             $source_array = $source->getPlurals();
             $translation_array = $string->getPlurals();
             if (count($source_array) == 1) {
                 // Add original string value and mark as non-plural.
                 $plural = FALSE;
                 $form['strings'][$string->lid]['original'] = array('#type' => 'item', '#title' => $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))), '#title_display' => 'invisible', '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>');
             } else {
                 // Add original string value and mark as plural.
                 $plural = TRUE;
                 $original_singular = ['#type' => 'item', '#title' => $this->t('Singular form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>', '#prefix' => '<span class="visually-hidden">' . $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))) . '</span>'];
                 $original_plural = ['#type' => 'item', '#title' => $this->t('Plural form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[1]) . '</span>'];
                 $form['strings'][$string->lid]['original'] = [$original_singular, ['#markup' => '<br>'], $original_plural];
             }
             if (!empty($string->context)) {
                 $form['strings'][$string->lid]['original'][] = ['#type' => 'inline_template', '#template' => '<br><small>{{ context_title }}: <span lang="en">{{ context }}</span></small>', '#context' => ['context_title' => $this->t('In Context'), 'context' => $string->context]];
             }
             // Approximate the number of rows to use in the default textarea.
             $rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
             if (!$plural) {
                 $form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Translated string (@language)', array('@language' => $langname)), '#title_display' => 'invisible', '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode));
             } else {
                 // Dealing with plural strings.
                 if (isset($plural_formulas[$langcode]['plurals']) && $plural_formulas[$langcode]['plurals'] > 2) {
                     // Add a textarea for each plural variant.
                     for ($i = 0; $i < $plural_formulas[$langcode]['plurals']; $i++) {
                         $form['strings'][$string->lid]['translations'][$i] = array('#type' => 'textarea', '#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '', '#attributes' => array('lang' => $langcode), '#prefix' => $i == 0 ? '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>' : '');
                     }
                 } else {
                     // Fallback for unknown number of plurals.
                     $form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Singular form'), '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode), '#prefix' => '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>');
                     $form['strings'][$string->lid]['translations'][1] = array('#type' => 'textarea', '#title' => $this->t('Plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '', '#attributes' => array('lang' => $langcode));
                 }
             }
         }
         if (count(Element::children($form['strings']))) {
             $form['actions'] = array('#type' => 'actions');
             $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save translations'));
         }
     }
     $form['pager']['#type'] = 'pager';
     return $form;
 }
Exemplo n.º 9
2
 /**
  * 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;
 }
Exemplo n.º 10
2
 /**
  * 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;
 }
Exemplo n.º 11
2
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, CartInterface $cart = NULL)
 {
     $form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
     $cart_config = $this->config('uc_cart.settings');
     $form['items'] = array('#type' => 'table', '#tree' => TRUE, '#header' => array('remove' => array('data' => $this->t('Remove'), 'class' => array('remove')), 'image' => array('data' => $this->t('Products'), 'class' => array('image')), 'desc' => array('data' => '', 'class' => array('desc')), 'qty' => array('data' => $this->t('Quantity'), 'class' => array('qty')), 'total' => array('data' => $this->t('Total'), 'class' => array('price'))));
     $form['data'] = array('#tree' => TRUE, '#parents' => array('items'));
     $i = 0;
     $subtotal = 0;
     foreach ($cart->getContents() as $cart_item) {
         $item = \Drupal::moduleHandler()->invoke($cart_item->data->module, 'uc_cart_display', array($cart_item));
         if (Element::children($item)) {
             $form['items'][$i]['remove'] = $item['remove'];
             $form['items'][$i]['remove']['#name'] = 'remove-' . $i;
             $form['items'][$i]['image'] = uc_product_get_picture($item['nid']['#value'], 'uc_cart');
             $form['items'][$i]['desc']['title'] = $item['title'];
             $form['items'][$i]['desc']['description'] = $item['description'];
             $form['items'][$i]['qty'] = $item['qty'];
             $form['items'][$i]['total'] = array('#theme' => 'uc_price', '#price' => $item['#total'], '#wrapper_attributes' => array('class' => 'total'));
             if (!empty($item['#suffixes'])) {
                 $form['items'][$i]['total']['#suffixes'] = $item['#suffixes'];
             }
             $form['data'][$i]['module'] = $item['module'];
             $form['data'][$i]['nid'] = $item['nid'];
             $form['data'][$i]['data'] = $item['data'];
             $form['data'][$i]['title'] = array('#type' => 'value', '#value' => $item['title']['#markup']);
             $subtotal += $item['#total'];
         }
         $i++;
     }
     $form['items'][]['total'] = array('#theme' => 'uc_price', '#prefix' => '<span id="subtotal-title">' . $this->t('Subtotal') . ':</span> ', '#price' => $subtotal, '#wrapper_attributes' => array('colspan' => 5, 'class' => array('subtotal')));
     $form['actions'] = array('#type' => 'actions');
     // If the continue shopping element is enabled...
     if (($cs_type = $cart_config->get('continue_shopping_type')) !== 'none') {
         // Add the element to the form based on the element type.
         if ($cart_config->get('continue_shopping_type') == 'link') {
             $form['actions']['continue_shopping'] = array('#markup' => $this->l($this->t('Continue shopping'), Url::fromUri('internal:' . $this->continueShoppingUrl())));
         } elseif ($cart_config->get('continue_shopping_type') == 'button') {
             $form['actions']['continue_shopping'] = array('#type' => 'submit', '#value' => $this->t('Continue shopping'), '#submit' => array(array($this, 'submitForm'), array($this, 'continueShopping')));
         }
     }
     // Add the empty cart button if enabled.
     if ($cart_config->get('empty_button')) {
         $form['actions']['empty'] = array('#type' => 'submit', '#value' => $this->t('Empty cart'), '#submit' => array(array($this, 'emptyCart')));
     }
     // Add the control buttons for updating and proceeding to checkout.
     $form['actions']['update'] = array('#type' => 'submit', '#name' => 'update-cart', '#value' => $this->t('Update cart'), '#submit' => array(array($this, 'submitForm'), array($this, 'displayUpdateMessage')));
     $form['actions']['checkout'] = array('#theme' => 'uc_cart_checkout_buttons');
     if ($cart_config->get('checkout_enabled')) {
         $form['actions']['checkout']['checkout'] = array('#type' => 'submit', '#value' => $this->t('Checkout'), '#button_type' => 'primary', '#submit' => array(array($this, 'submitForm'), array($this, 'checkout')));
     }
     $this->renderer->addCacheableDependency($form, $cart);
     $this->renderer->addCacheableDependency($form, $cart_config);
     return $form;
 }
Exemplo n.º 12
2
 /**
  * Stores the errors of each element directly on the element.
  *
  * We must provide a way for non-form functions to check the errors for a
  * specific element. The most common usage of this is a #pre_render callback.
  *
  * @param array $elements
  *   An associative array containing the structure of a form element.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  */
 protected function setElementErrorsFromFormState(array &$elements, FormStateInterface &$form_state)
 {
     // Recurse through all children.
     foreach (Element::children($elements) as $key) {
         if (isset($elements[$key]) && $elements[$key]) {
             $this->setElementErrorsFromFormState($elements[$key], $form_state);
         }
     }
     // Store the errors for this element on the element directly.
     $elements['#errors'] = $form_state->getError($elements);
 }
Exemplo n.º 13
1
 /**
  * {@inheritdoc}
  */
 public function preRender(&$element, $rendering_object)
 {
     $element += array('#prefix' => '<div class=" ' . implode(' ', $this->getClasses()) . '">', '#suffix' => '</div>', '#tree' => TRUE, '#parents' => array($this->group->group_name), '#default_tab' => '');
     if ($this->getSetting('id')) {
         $element['#id'] = Html::getId($this->getSetting('id'));
     }
     // By default tabs don't have titles but you can override it in the theme.
     if ($this->getLabel()) {
         $element['#title'] = SafeMarkup::checkPlain($this->getLabel());
     }
     $form_state = new \Drupal\Core\Form\FormState();
     if ($this->getSetting('direction') == 'vertical') {
         $element += array('#type' => 'vertical_tabs', '#theme_wrappers' => array('vertical_tabs'));
         $complete_form = array();
         $element = \Drupal\Core\Render\Element\VerticalTabs::processVerticalTabs($element, $form_state, $complete_form);
     } else {
         $element += array('#type' => 'horizontal_tabs', '#theme_wrappers' => array('horizontal_tabs'));
         $on_form = $this->context == 'form';
         $element = \Drupal\field_group\Element\HorizontalTabs::processHorizontalTabs($element, $form_state, $on_form);
     }
     // Make sure the group has 1 child. This is needed to succeed at form_pre_render_vertical_tabs().
     // Skipping this would force us to move all child groups to this array, making it an un-nestable.
     $element['group']['#groups'][$this->group->group_name] = array(0 => array());
     $element['group']['#groups'][$this->group->group_name]['#group_exists'] = TRUE;
     // Search for a tab that was marked as open. First one wins.
     foreach (\Drupal\Core\Render\Element::children($element) as $tab_name) {
         if (!empty($element[$tab_name]['#open'])) {
             $element[$this->group->group_name . '__active_tab']['#default_value'] = $tab_name;
             break;
         }
     }
 }
Exemplo n.º 14
1
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $forum_config = $this->config('forum.settings');
     $vid = $forum_config->get('vocabulary');
     $vocabulary = $this->entityManager->getStorage('taxonomy_vocabulary')->load($vid);
     if (!$vocabulary) {
         throw new NotFoundHttpException();
     }
     // Build base taxonomy term overview.
     $form = parent::buildForm($form, $form_state, $vocabulary);
     foreach (Element::children($form['terms']) as $key) {
         if (isset($form['terms'][$key]['#term'])) {
             $term = $form['terms'][$key]['#term'];
             $form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);
             unset($form['terms'][$key]['operations']['#links']['delete']);
             $route_parameters = $form['terms'][$key]['operations']['#links']['edit']['url']->getRouteParameters();
             if (!empty($term->forum_container->value)) {
                 $form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit container');
                 $form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', $route_parameters);
             } else {
                 $form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit forum');
                 $form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_form', $route_parameters);
             }
             // We don't want the redirect from the link so we can redirect the
             // delete action.
             unset($form['terms'][$key]['operations']['#links']['edit']['query']['destination']);
         }
     }
     // Remove the alphabetical reset.
     unset($form['actions']['reset_alphabetical']);
     // Use the existing taxonomy overview submit handler.
     $form['terms']['#empty'] = $this->t('No containers or forums available. <a href="@container">Add container</a> or <a href="@forum">Add forum</a>.', array('@container' => $this->url('forum.add_container'), '@forum' => $this->url('forum.add_forum')));
     return $form;
 }
Exemplo n.º 15
1
 /**
  * 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;
 }
Exemplo n.º 16
1
 /**
  * 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;
 }
Exemplo n.º 17
1
 /**
  * Will replace placeholders in the #text offsets.
  *
  * @param array $data
  *   Data structures where to replace placeholders.
  * @param $variables
  *   Key value pairs.
  */
 protected function replacePlaceholders(&$data, $variables)
 {
     foreach (Element::children($data) as $key) {
         if (isset($data[$key]['#text'])) {
             $data[$key]['#text'] = (string) new FormattableMarkup($data[$key]['#text'], $variables);
         } else {
             $this->replacePlaceholders($data[$key], $variables);
         }
     }
 }
Exemplo n.º 18
1
 /**
  * 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;
 }
Exemplo n.º 19
1
 /**
  * {@inheritdoc}
  */
 public function build()
 {
     // Get block
     $block = $this->getBlock();
     // Apply block config.
     $block_config = $this->blockConfig();
     $block->setConfiguration($block_config);
     // Get render array.
     $block_elements = $block->build();
     // Return an empty array if there is nothing to render.
     return Element::isEmpty($block_elements) ? [] : $block_elements;
 }
Exemplo n.º 20
0
 /**
  * Loops through and displays all form errors.
  *
  * @param array $form
  *   An associative array containing the structure of the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  */
 protected function displayErrorMessages(array $form, FormStateInterface $form_state)
 {
     $error_links = [];
     $errors = $form_state->getErrors();
     // Loop through all form errors and check if we need to display a link.
     foreach ($errors as $name => $error) {
         $form_element = FormElementHelper::getElementByName($name, $form);
         $title = FormElementHelper::getElementTitle($form_element);
         // Only show links to erroneous elements that are visible.
         $is_visible_element = Element::isVisibleElement($form_element);
         // Only show links for elements that have a title themselves or have
         // children with a title.
         $has_title = !empty($title);
         // Only show links for elements with an ID.
         $has_id = !empty($form_element['#id']);
         // Do not show links to elements with suppressed messages. Most often
         // their parent element is used for inline errors.
         if (!empty($form_element['#error_no_message'])) {
             unset($errors[$name]);
         } elseif ($is_visible_element && $has_title && $has_id) {
             $error_links[] = $this->l($title, Url::fromRoute('<none>', [], ['fragment' => $form_element['#id'], 'external' => TRUE]));
             unset($errors[$name]);
         }
     }
     // Set normal error messages for all remaining errors.
     foreach ($errors as $error) {
         $this->drupalSetMessage($error, 'error');
     }
     if (!empty($error_links)) {
         $render_array = [['#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: ')], ['#theme' => 'item_list', '#items' => $error_links, '#context' => ['list_style' => 'comma-list']]];
         $message = $this->renderer->renderPlain($render_array);
         $this->drupalSetMessage($message, 'error');
     }
 }
Exemplo n.º 21
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;
 }
Exemplo n.º 22
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;
 }
Exemplo n.º 23
0
 /**
  * Prepares a vertical_tabs element for rendering.
  *
  * @param array $element
  *   An associative array containing the properties and children of the
  *   vertical tabs element.
  *
  * @return array
  *   The modified element.
  */
 public static function preRenderVerticalTabs($element)
 {
     // Do not render the vertical tabs element if it is empty.
     $group = implode('][', $element['#parents']);
     if (!Element::getVisibleChildren($element['group']['#groups'][$group])) {
         $element['#printed'] = TRUE;
     }
     return $element;
 }
Exemplo n.º 24
0
/**
 * 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;
    }
}
Exemplo n.º 25
-1
 /**
  * Builds the Toolbar as a structured array ready for drupal_render().
  *
  * Since building the toolbar takes some time, it is done just prior to
  * rendering to ensure that it is built only if it will be displayed.
  *
  * @param array $element
  *   A renderable array.
  *
  * @return array
  *  A renderable array.
  *
  * @see toolbar_page_top().
  */
 public static function preRenderToolbar($element)
 {
     // Get the configured breakpoints to switch from vertical to horizontal
     // toolbar presentation.
     $breakpoints = static::breakpointManager()->getBreakpointsByGroup('toolbar');
     if (!empty($breakpoints)) {
         $media_queries = array();
         foreach ($breakpoints as $id => $breakpoint) {
             $media_queries[$id] = $breakpoint->getMediaQuery();
         }
         $element['#attached']['drupalSettings']['toolbar']['breakpoints'] = $media_queries;
     }
     $module_handler = static::moduleHandler();
     // Get toolbar items from all modules that implement hook_toolbar().
     $items = $module_handler->invokeAll('toolbar');
     // Allow for altering of hook_toolbar().
     $module_handler->alter('toolbar', $items);
     // Sort the children.
     uasort($items, array('\\Drupal\\Component\\Utility\\SortArray', 'sortByWeightProperty'));
     // Merge in the original toolbar values.
     $element = array_merge($element, $items);
     // Assign each item a unique ID, based on its key.
     foreach (Element::children($element) as $key) {
         $element[$key]['#id'] = Html::getId('toolbar-item-' . $key);
     }
     return $element;
 }
Exemplo n.º 26
-1
 /**
  * Rewrite #states selectors.
  *
  * @param array $elements
  *   A renderable array element having a #states property.
  * @param string $search
  *   A partial or entire jQuery selector string to replace in #states.
  * @param string $replace
  *   The string to replace all instances of $search with.
  *
  * @see drupal_process_states()
  */
 public static function rewriteStatesSelector(array &$elements, $search, $replace)
 {
     if (!empty($elements['#states'])) {
         foreach ($elements['#states'] as $state => $ids) {
             static::processStatesArray($elements['#states'][$state], $search, $replace);
         }
     }
     foreach (Element::children($elements) as $key) {
         static::rewriteStatesSelector($elements[$key], $search, $replace);
     }
 }
Exemplo n.º 27
-1
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $config = $this->config('cosign.settings');
     foreach (Element::children($form) as $variable) {
         $config->set($variable, $form_state->getValue($form[$variable]['#parents']));
     }
     $config->save();
     if (method_exists($this, '_submitForm')) {
         $this->_submitForm($form, $form_state);
     }
     parent::submitForm($form, $form_state);
 }
Exemplo n.º 28
-1
 /**
  * {@inheritdoc}
  */
 public function view(FieldItemListInterface $items)
 {
     $elements = $this->viewElements($items);
     // If there are actual renderable children, use #theme => field, otherwise,
     // let access cacheability metadata pass through for correct bubbling.
     if (Element::children($elements)) {
         $entity = $items->getEntity();
         $entity_type = $entity->getEntityTypeId();
         $field_name = $this->fieldDefinition->getName();
         $info = array('#theme' => 'field', '#title' => $this->fieldDefinition->getLabel(), '#label_display' => $this->label, '#view_mode' => $this->viewMode, '#language' => $items->getLangcode(), '#field_name' => $field_name, '#field_type' => $this->fieldDefinition->getType(), '#field_translatable' => $this->fieldDefinition->isTranslatable(), '#entity_type' => $entity_type, '#bundle' => $entity->bundle(), '#object' => $entity, '#items' => $items, '#formatter' => $this->getPluginId());
         $elements = array_merge($info, $elements);
     }
     return $elements;
 }
Exemplo n.º 29
-1
 /**
  * Returns the title for the element.
  *
  * If the element has no title, this will recurse through all children of the
  * element until a title is found.
  *
  * @param array $element
  *   An associative array containing the properties of the form element.
  *
  * @return string
  *   The title of the element, or an empty string if none is found.
  */
 public static function getElementTitle(array $element)
 {
     $title = '';
     if (isset($element['#title'])) {
         $title = $element['#title'];
     } else {
         foreach (Element::children($element) as $key) {
             if ($title = static::getElementTitle($element[$key])) {
                 break;
             }
         }
     }
     return $title;
 }
Exemplo n.º 30
-1
 /**
  * Implements preRenderAddThisWrapper()
  *   - Defines consistent markup for the addthis_wrapper render element.
  * @param $element
  * @return mixed
  */
 public static function preRenderAddThisWrapper($element)
 {
     $tag_open = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>';
     $children = Element::children($element);
     $output = '';
     if (count($children) > 0) {
         foreach ($children as $child) {
             $output .= render($element[$child]);
         }
     }
     $tag_close = '</' . $element['#tag'] . ">  \n";
     $element['#markup'] = $tag_open . $output . $tag_close;
     return $element;
 }