diff --git a/core/modules/field_ui/lib/Drupal/field_ui/FieldInstanceListController.php b/core/modules/field_ui/lib/Drupal/field_ui/FieldInstanceListController.php index e807d68..b7c9048 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/FieldInstanceListController.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/FieldInstanceListController.php @@ -151,11 +151,11 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $field_instance) { - $row['label'] = String::checkPlain($field_instance->getFieldLabel()); - $row['machine_name'] = $field_instance->getFieldName(); + $row['label'] = String::checkPlain($field_instance->getLabel()); + $row['machine_name'] = $field_instance->getName(); $field_type = array( '#type' => 'link', - '#title' => $this->fieldTypes[$field_instance->getField()->getFieldType()]['label'], + '#title' => $this->fieldTypes[$field_instance->getField()->getType()]['label'], '#href' => $this->currentPath . '/' . $field_instance->id() . '/field', '#options' => array('attributes' => array('title' => $this->t('Edit field settings.'))), ); diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldAddForm.php b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldAddForm.php index 32ac16b..28d42ab 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldAddForm.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldAddForm.php @@ -12,7 +12,6 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldTypePluginManager; use Drupal\Core\Form\FormBase; -use Drupal\Core\Form\FormBuilderInterface; use Drupal\field\Entity\Field; use Drupal\field\FieldInfo; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -72,13 +71,6 @@ class FieldAddForm extends FormBase { protected $fieldTypePluginManager; /** - * The form builder being tested. - * - * @var \Drupal\Core\Form\FormBuilderInterface - */ - protected $formBuilder; - - /** * Constructs a new FieldAddForm object. * * @param \Drupal\Core\Entity\EntityManager $entity_manager @@ -87,14 +79,11 @@ class FieldAddForm extends FormBase { * The field info service. * @param \Drupal\Core\Field\FieldTypePluginManager $field_type_plugin_manager * The field type plugin manager. - * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The form builder. */ - public function __construct(EntityManager $entity_manager, FieldInfo $field_info, FieldTypePluginManager $field_type_plugin_manager, FormBuilderInterface $form_builder) { + public function __construct(EntityManager $entity_manager, FieldInfo $field_info, FieldTypePluginManager $field_type_plugin_manager) { $this->entityManager = $entity_manager; $this->fieldInfo = $field_info; $this->fieldTypePluginManager = $field_type_plugin_manager; - $this->formBuilder = $form_builder; } /** @@ -155,6 +144,10 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, '#options' => $field_type_options, '#empty_option' => $this->t('- Select a field type -'), '#description' => $this->t('Type of data to store.'), + '#ajax' => array( + 'callback' => array(get_class($this), 'updateForm'), + 'wrapper' => 'field-add-wrapper', + ), ); $form['label'] = array( @@ -190,27 +183,12 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, '#value' => FALSE, ); - // Determine which options from the 'type' field already have existing - // fields and add them to the $states array so we can determine if the - // new-or-existing select list should be displayed. - $existing_fields = $this->getExistingFieldOptions(); + // Determine which options from the 'type' element already have existing + // fields so we know if the new-or-existing element should be displayed. + $field_type = isset($form_state['values']['type']) ? $form_state['values']['type'] : NULL; + $existing_fields = $this->getExistingFieldOptions($field_type); - $states[] = array('value' => FALSE); - foreach ($existing_fields as $field) { - $states[] = array('value' => $field['type']); - } - - // #states don't work if they are used on a 'radios' element so we have to - // use a wrapper. - $form['new_or_existing_wrapper'] = array( - '#type' => 'container', - '#states' => array( - 'visible' => array( - 'select[name="type"]' => $states, - ), - ), - ); - $form['new_or_existing_wrapper']['new_or_existing'] = array( + $form['new_or_existing'] = array( '#type' => 'radios', '#options' => array( 'new' => $this->t('Create a new field'), @@ -222,19 +200,14 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, 'callback' => array(get_class($this), 'updateForm'), 'wrapper' => 'field-add-wrapper', ), + '#access' => !empty($existing_fields), ); // Update the 'field_name' element if we are adding a new instance of an // existing field. - if (isset($form_state['values']['new_or_existing']) && $form_state['values']['new_or_existing'] == 'existing') { + if (!empty($existing_fields) && isset($form_state['values']['new_or_existing']) && $form_state['values']['new_or_existing'] == 'existing') { $existing_field_options = array(); foreach ($existing_fields as $field_name => $info) { - // Show only existing fields of the same type as the one selected by the - // user. - if (isset($form_state['values']['type']) && $form_state['values']['type'] != $info['type']) { - continue; - } - $text = $this->t('@type: @field (@label)', array( '@type' => $info['type_label'], '@label' => $info['label'], @@ -278,7 +251,7 @@ public function validateForm(array &$form, array &$form_state) { // Missing field type. if (!$values['type']) { - $this->formBuilder->setErrorByName('type', $form_state, $this->t('You need to select a field type.')); + $this->setFormError('type', $form_state, $this->t('You need to select a field type.')); } $this->validateAddNew($form, $form_state); @@ -302,12 +275,12 @@ protected function validateAddNew(array $form, array &$form_state) { if ($values['new_or_existing'] == 'new' && array_filter(array($values['label'], $values['field_name']))) { // Missing label. if (!$values['label']) { - $this->formBuilder->setErrorByName('label', $form_state, $this->t('You need to provide a label.')); + $this->setFormError('label', $form_state, $this->t('You need to provide a label.')); } // Missing field name. if (!$values['field_name']) { - $this->formBuilder->setErrorByName('field_name', $form_state, $this->t('You need to provide a field name.')); + $this->setFormError('field_name', $form_state, $this->t('You need to provide a field name.')); } // Field name validation. else { @@ -316,7 +289,7 @@ protected function validateAddNew(array $form, array &$form_state) { // Check if the field name is not longer then the max_length. if(Field::NAME_MAX_LENGTH - (strlen($field_prefix) + strlen($field_name)) < 0) { - $this->formBuilder->setErrorByName('field_name', $form_state, $this->t('New field name cannot be longer than %max_length characters but is currently %current_length characters long.', array('%max_length' => Field::NAME_MAX_LENGTH - strlen($field_prefix), 'current_length' => strlen($field_name)))); + $this->setFormError('field_name', $form_state, $this->t('New field name cannot be longer than %max_length characters but is currently %current_length characters long.', array('%max_length' => Field::NAME_MAX_LENGTH - strlen($field_prefix), 'current_length' => strlen($field_name)))); } } } @@ -339,12 +312,12 @@ protected function validateAddExisting(array $form, array &$form_state) { if ($values['new_or_existing'] == 'existing' && array_filter(array($values['label'], $values['field_name']))) { // Missing label. if (!$values['label']) { - $this->formBuilder->setErrorByName('label', $form_state, $this->t('You need to provide a label.')); + $this->setFormError('label', $form_state, $this->t('You need to provide a label.')); } // Missing existing field name. if (!$values['field_name']) { - $this->formBuilder->setErrorByName('field_name', $form_state, $this->t('You need to select an existing field.')); + $this->setFormError('field_name', $form_state, $this->t('You need to select an existing field.')); } } } @@ -401,7 +374,7 @@ public function submitForm(array &$form, array &$form_state) { } else { $instance = array( - 'field_name' => $field->getFieldName(), + 'field_name' => $field->getName(), 'entity_type' => $this->entityType, 'bundle' => $this->bundle, 'label' => $values['label'], @@ -447,24 +420,27 @@ protected function configureEntityDisplays(FieldDefinitionInterface $field) { // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. entity_get_form_display($this->entityType, $this->bundle, 'default') - ->setComponent($field->getFieldName()) + ->setComponent($field->getName()) ->save(); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($this->entityType, $this->bundle, 'default') - ->setComponent($field->getFieldName()) + ->setComponent($field->getName()) ->save(); } /** * Returns an array of existing fields to be added to a bundle. * + * @param string $field_type + * A field type. + * * @return array * An array of existing fields keyed by field name. */ - protected function getExistingFieldOptions() { + protected function getExistingFieldOptions($field_type) { $options = array(); // Collect candidate field instances: all instances of fields for this @@ -473,7 +449,7 @@ protected function getExistingFieldOptions() { $instance_ids = array(); if (!empty($field_map[$this->entityType])) { foreach ($field_map[$this->entityType] as $field_name => $data) { - if (!in_array($this->bundle, $data['bundles'])) { + if ($data['type'] == $field_type && !in_array($this->bundle, $data['bundles'])) { $bundle = reset($data['bundles']); $instance_ids[] = $this->entityType . '.' . $bundle . '.' . $field_name; } @@ -488,14 +464,14 @@ protected function getExistingFieldOptions() { // Do not show: // - locked fields, // - fields that should not be added via user interface. - $field_type = $instance->getFieldType(); + $field_type = $instance->getType(); $field = $instance->getField(); if (empty($field->locked) && empty($field_types[$field_type]['no_ui'])) { - $options[$instance->getFieldName()] = array( + $options[$instance->getName()] = array( 'type' => $field_type, 'type_label' => $field_types[$field_type]['label'], - 'field' => $instance->getFieldName(), - 'label' => $instance->getFieldLabel(), + 'field' => $instance->getName(), + 'label' => $instance->getLabel(), ); } }