/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $field_storage = $this->entity->getFieldStorageDefinition(); $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId()); $form_title = $this->t('%field settings for %bundle', array('%field' => $this->entity->getLabel(), '%bundle' => $bundles[$this->entity->bundle]['label'])); $form['#title'] = $form_title; if ($field_storage->isLocked()) { $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->entity->getLabel()))); return $form; } // Build the configurable field values. $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $this->entity->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20); $form['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->entity->getDescription(), '#rows' => 5, '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => $this->displayAllowedTags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10); $form['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->entity->isRequired(), '#weight' => -5); // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->entity->getTargetEntityTypeId(), 'bundle' => $this->entity->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $items = $form['#entity']->get($this->entity->getName()); $item = $items->first() ?: $items->appendItem(); // Add field settings for the field type and a container for third party // settings that modules can add to via hook_form_FORM_ID_alter(). $form['settings'] = array('#tree' => TRUE, '#weight' => 10); $form['settings'] += $item->fieldSettingsForm($form, $form_state); $form['third_party_settings'] = array('#tree' => TRUE, '#weight' => 11); // Add handling for default value. if ($element = $items->defaultValuesForm($form, $form_state)) { $element = array_merge($element, array('#type' => 'details', '#title' => $this->t('Default value'), '#open' => TRUE, '#tree' => TRUE, '#description' => $this->t('The default value for this field, used when creating new content.'))); $form['default_value'] = $element; } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, FieldConfigInterface $field_config = NULL) { $this->field = $field_config; $form_state->set('field', $field_config); $bundle = $this->field->bundle; $entity_type = $this->field->entity_type; $field_storage = $this->field->getFieldStorageDefinition(); $bundles = entity_get_bundles(); $form_title = $this->t('%field settings for %bundle', array('%field' => $this->field->getLabel(), '%bundle' => $bundles[$entity_type][$bundle]['label'])); $form['#title'] = $form_title; $form['#field'] = $field_storage; // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->field->entity_type, 'bundle' => $this->field->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $items = $form['#entity']->get($this->field->getName()); $item = $items->first() ?: $items->appendItem(); if (!empty($field_storage->locked)) { $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->field->getLabel()))); return $form; } // Create a form structure for the field values. $form['field'] = array('#tree' => TRUE); // Build the non-configurable field values. $form['field']['field_name'] = array('#type' => 'value', '#value' => $this->field->getName()); $form['field']['entity_type'] = array('#type' => 'value', '#value' => $entity_type); $form['field']['bundle'] = array('#type' => 'value', '#value' => $bundle); // Build the configurable field values. $form['field']['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $this->field->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20); $form['field']['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->field->getDescription(), '#rows' => 5, '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => $this->displayAllowedTags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10); $form['field']['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->field->isRequired(), '#weight' => -5); // Add field settings for the field type and a container for third party // settings that modules can add to via hook_form_FORM_ID_alter(). $form['field']['settings'] = $item->fieldSettingsForm($form, $form_state); $form['field']['settings']['#weight'] = 10; $form['field']['third_party_settings'] = array(); $form['field']['third_party_settings']['#weight'] = 11; // Add handling for default value. if ($element = $items->defaultValuesForm($form, $form_state)) { $element = array_merge($element, array('#type' => 'details', '#title' => $this->t('Default value'), '#open' => TRUE, '#description' => $this->t('The default value for this field, used when creating new content.'))); $form['field']['default_value'] = $element; } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save settings'), '#button_type' => 'primary'); $form['actions']['delete'] = array('#type' => 'submit', '#value' => $this->t('Delete field'), '#submit' => array('::delete')); return $form; }