/**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     foreach ($items as $delta => $item) {
         $elements[$delta] = array('#markup' => field_filter_xss($item->value));
     }
     return $elements;
 }
示例#2
0
 public function summaryName($data)
 {
     $value = $data->{$this->name_alias};
     // If the list element has a human readable name show it,
     if (isset($this->allowed_values[$value]) && !empty($this->options['summary']['human'])) {
         return field_filter_xss($this->allowed_values[$value]);
     } else {
         return String::checkPlain($value);
     }
 }
示例#3
0
 /**
  * Overrides \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formMultipleElements().
  *
  * Special handling for draggable multiple widgets and 'add more' button.
  */
 protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $elements = parent::formMultipleElements($items, $form, $form_state);
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     $file_upload_help = array('#theme' => 'file_upload_help', '#description' => '', '#upload_validators' => $elements[0]['#upload_validators'], '#cardinality' => $cardinality);
     if ($cardinality == 1) {
         // If there's only one field, return it as delta 0.
         if (empty($elements[0]['#default_value']['fids'])) {
             $file_upload_help['#description'] = field_filter_xss($this->fieldDefinition->getDescription());
             $elements[0]['#description'] = drupal_render($file_upload_help);
         }
     } else {
         $elements['#file_upload_description'] = $file_upload_help;
     }
     return $elements;
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     $entity = $items->getEntity();
     $allowed_values = options_allowed_values($this->fieldDefinition, $entity);
     foreach ($items as $delta => $item) {
         if (isset($allowed_values[$item->value])) {
             $output = field_filter_xss($allowed_values[$item->value]);
         } else {
             // If no match was found in allowed values, fall back to the key.
             $output = field_filter_xss($item->value);
         }
         $elements[$delta] = array('#markup' => $output);
     }
     return $elements;
 }
示例#5
0
 /**
  * Sanitizes a string label to display as an option.
  *
  * @param string $label
  *   The label to sanitize.
  */
 protected static function sanitizeLabel(&$label)
 {
     // Allow a limited set of HTML tags.
     $label = field_filter_xss($label);
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state)
 {
     $value = isset($items[$delta]->value) ? $items[$delta]->value : NULL;
     $field_settings = $this->getFieldSettings();
     $element += array('#type' => 'number', '#default_value' => $value, '#placeholder' => $this->getSetting('placeholder'));
     // Set the step for floating point and decimal numbers.
     switch ($this->fieldDefinition->getType()) {
         case 'decimal':
             $element['#step'] = pow(0.1, $field_settings['scale']);
             break;
         case 'float':
             $element['#step'] = 'any';
             break;
     }
     // Set minimum and maximum.
     if (is_numeric($field_settings['min'])) {
         $element['#min'] = $field_settings['min'];
     }
     if (is_numeric($field_settings['max'])) {
         $element['#max'] = $field_settings['max'];
     }
     // Add prefix and suffix.
     if ($field_settings['prefix']) {
         $prefixes = explode('|', $field_settings['prefix']);
         $element['#field_prefix'] = field_filter_xss(array_pop($prefixes));
     }
     if ($field_settings['suffix']) {
         $suffixes = explode('|', $field_settings['suffix']);
         $element['#field_suffix'] = field_filter_xss(array_pop($suffixes));
     }
     return array('value' => $element);
 }
示例#7
0
 /**
  * Special handling to create form elements for multiple values.
  *
  * Handles generic features for multiple fields:
  * - number of widgets
  * - AHAH-'add more' button
  * - table display and drag-n-drop value reordering
  */
 protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     $parents = $form['#parents'];
     // Determine the number of widgets to display.
     switch ($cardinality) {
         case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
             $field_state = static::getWidgetState($parents, $field_name, $form_state);
             $max = $field_state['items_count'];
             $is_multiple = TRUE;
             break;
         default:
             $max = $cardinality - 1;
             $is_multiple = $cardinality > 1;
             break;
     }
     $title = String::checkPlain($this->fieldDefinition->getLabel());
     $description = field_filter_xss(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
     $elements = array();
     for ($delta = 0; $delta <= $max; $delta++) {
         // For multiple fields, title and description are handled by the wrapping
         // table.
         $element = array('#title' => $is_multiple ? '' : $title, '#description' => $is_multiple ? '' : $description);
         $element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
         if ($element) {
             // Input field for the delta (drag-n-drop reordering).
             if ($is_multiple) {
                 // We name the element '_weight' to avoid clashing with elements
                 // defined by widget.
                 $element['_weight'] = array('#type' => 'weight', '#title' => t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', '#delta' => $max, '#default_value' => $items[$delta]->_weight ?: $delta, '#weight' => 100);
             }
             $elements[$delta] = $element;
         }
     }
     if ($elements) {
         $elements += array('#theme' => 'field_multiple_value_form', '#field_name' => $field_name, '#cardinality' => $cardinality, '#cardinality_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(), '#required' => $this->fieldDefinition->isRequired(), '#title' => $title, '#description' => $description, '#max_delta' => $max);
         // Add 'add more' button, if not working with a programmed form.
         if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && empty($form_state['programmed'])) {
             $id_prefix = implode('-', array_merge($parents, array($field_name)));
             $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper');
             $elements['#prefix'] = '<div id="' . $wrapper_id . '">';
             $elements['#suffix'] = '</div>';
             $elements['add_more'] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', '#value' => t('Add another item'), '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'));
         }
     }
     return $elements;
 }
示例#8
0
 /**
  * Overrides \Drupal\Core\Field\WidgetBase::formMultipleElements().
  *
  * Special handling for draggable multiple widgets and 'add more' button.
  */
 protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $parents = $form['#parents'];
     // Load the items for form rebuilds from the field state as they might not be
     // in $form_state['values'] because of validation limitations. Also, they are
     // only passed in as $items when editing existing entities.
     $field_state = static::getWidgetState($parents, $field_name, $form_state);
     if (isset($field_state['items'])) {
         $items->setValue($field_state['items']);
     }
     // Determine the number of widgets to display.
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     switch ($cardinality) {
         case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
             $max = count($items);
             $is_multiple = TRUE;
             break;
         default:
             $max = $cardinality - 1;
             $is_multiple = $cardinality > 1;
             break;
     }
     $title = String::checkPlain($this->fieldDefinition->getLabel());
     $description = field_filter_xss($this->fieldDefinition->getDescription());
     $elements = array();
     $delta = 0;
     // Add an element for every existing item.
     foreach ($items as $item) {
         $element = array('#title' => $title, '#description' => $description);
         $element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
         if ($element) {
             // Input field for the delta (drag-n-drop reordering).
             if ($is_multiple) {
                 // We name the element '_weight' to avoid clashing with elements
                 // defined by widget.
                 $element['_weight'] = array('#type' => 'weight', '#title' => t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', '#delta' => $max, '#default_value' => $item->_weight ?: $delta, '#weight' => 100);
             }
             $elements[$delta] = $element;
             $delta++;
         }
     }
     $empty_single_allowed = $cardinality == 1 && $delta == 0;
     $empty_multiple_allowed = ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $delta < $cardinality) && empty($form_state['programmed']);
     // Add one more empty row for new uploads except when this is a programmed
     // multiple form as it is not necessary.
     if ($empty_single_allowed || $empty_multiple_allowed) {
         $element = array('#title' => $title, '#description' => $description);
         $element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
         if ($element) {
             $element['#required'] = $element['#required'] && $delta == 0;
             $elements[$delta] = $element;
         }
     }
     if ($is_multiple) {
         // The group of elements all-together need some extra functionality after
         // building up the full list (like draggable table rows).
         $elements['#file_upload_delta'] = $delta;
         $elements['#type'] = 'details';
         $elements['#open'] = TRUE;
         $elements['#theme'] = 'file_widget_multiple';
         $elements['#theme_wrappers'] = array('details');
         $elements['#process'] = array(array(get_class($this), 'processMultiple'));
         $elements['#title'] = $title;
         $elements['#description'] = $description;
         $elements['#field_name'] = $field_name;
         $elements['#language'] = $items->getLangcode();
         $elements['#display_field'] = (bool) $this->getFieldSetting('display_field');
         // The field settings include defaults for the field type. However, this
         // widget is a base class for other widgets (e.g., ImageWidget) that may
         // act on field types without these expected settings.
         $field_settings = $this->getFieldSettings() + array('display_field' => NULL);
         $elements['#display_field'] = (bool) $field_settings['display_field'];
         // Add some properties that will eventually be added to the file upload
         // field. These are added here so that they may be referenced easily
         // through a hook_form_alter().
         $elements['#file_upload_title'] = t('Add a new file');
         $elements['#file_upload_description'] = array('#theme' => 'file_upload_help', '#description' => '', '#upload_validators' => $elements[0]['#upload_validators'], '#cardinality' => $cardinality);
     }
     return $elements;
 }