ConfigFieldItemList.php 3.79 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\ConfigFieldItemList.
6 7
 */

8
namespace Drupal\Core\Field;
9 10

/**
11
 * Represents a configurable entity field item list.
12
 */
13
class ConfigFieldItemList extends FieldItemList implements ConfigFieldItemListInterface {
14 15 16 17 18

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
19
    $constraints = parent::getConstraints();
20 21 22
    // Check that the number of values doesn't exceed the field cardinality. For
    // form submitted values, this can only happen with 'multiple value'
    // widgets.
23
    $cardinality = $this->getFieldDefinition()->getCardinality();
24
    if ($cardinality != FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
25
      $constraints[] = \Drupal::typedDataManager()
26 27 28
        ->getValidationConstraintManager()
        ->create('Count', array(
          'max' => $cardinality,
29
          'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality)),
30 31 32 33 34 35
        ));
    }

    return $constraints;
  }

36 37 38 39
  /**
   * {@inheritdoc}
   */
  public function defaultValuesForm(array &$form, array &$form_state) {
40
    if (empty($this->getFieldDefinition()->default_value_function)) {
41
      // Place the input in a separate place in the submitted values tree.
42
      $widget = $this->defaultValueWidget($form_state);
43
      $element = array('#parents' => array('default_value_input'));
44
      $element += $widget->form($this, $element, $form_state);
45 46 47 48 49 50 51 52 53 54

      return $element;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) {
    // Extract the submitted value, and validate it.
55
    $widget = $this->defaultValueWidget($form_state);
56
    $widget->extractFormValues($this, $element, $form_state);
57 58 59 60
    $violations = $this->validate();

    if (count($violations)) {
      // Store reported errors in $form_state.
61
      $field_name = $this->getFieldDefinition()->getName();
62
      $field_state = field_form_get_state($element['#parents'], $field_name, $form_state);
63
      $field_state['constraint_violations'] = $violations;
64
      field_form_set_state($element['#parents'], $field_name, $form_state, $field_state);
65 66

      // Assign reported errors to the correct form element.
67
      $widget->flagErrors($this, $element, $form_state);
68 69 70 71 72 73 74 75
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) {
    // Extract the submitted value, and return it as an array.
76
    $widget = $this->defaultValueWidget($form_state);
77
    $widget->extractFormValues($this, $element, $form_state);
78 79 80 81 82 83 84 85 86
    return $this->getValue();
  }

  /**
   * Returns the widget object used in default value form.
   *
   * @param array $form_state
   *   The form state of the (entire) configuration form.
   *
87
   * @return \Drupal\Core\Field\WidgetInterface
88 89 90 91
   *   A Widget object.
   */
  protected function defaultValueWidget(array &$form_state) {
    if (!isset($form_state['default_value_widget'])) {
92
      $entity = $this->getEntity();
93 94 95 96 97 98 99

      // Force a non-required widget.
      $this->getFieldDefinition()->required = FALSE;
      $this->getFieldDefinition()->description = '';

      // Use the widget currently configured for the 'default' form mode, or
      // fallback to the default widget for the field type.
100
      $entity_form_display = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'default');
101
      $widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getName());
102 103 104 105 106 107 108 109 110 111
      if (!$widget) {
        $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition()));
      }

      $form_state['default_value_widget'] = $widget;
    }

    return $form_state['default_value_widget'];
  }

112
}