public function FieldInstanceEditForm::buildForm

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

array $form_state: An associative array containing the current state of the form.

Return value

array The form structure.

Overrides FieldInstanceFormBase::buildForm

File

drupal/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php, line 28
Contains \Drupal\field_ui\Form\FieldInstanceEditForm.

Class

FieldInstanceEditForm
Provides a form for the field instance settings form.

Namespace

Drupal\field_ui\Form

Code

public function buildForm(array $form, array &$form_state, FieldInstanceInterface $field_instance = NULL) {
  parent::buildForm($form, $form_state, $field_instance);
  $bundle = $this->instance['bundle'];
  $entity_type = $this->instance['entity_type'];
  $field = $this->instance
    ->getField();
  $entity_form_display = entity_get_form_display($entity_type, $bundle, 'default');
  $bundles = entity_get_bundles();
  drupal_set_title(t('%instance settings for %bundle', array(
    '%instance' => $this->instance
      ->label(),
    '%bundle' => $bundles[$entity_type][$bundle]['label'],
  )), PASS_THROUGH);
  $form['#field'] = $field;
  $form['#entity_form_display'] = $entity_form_display;

  // Create an arbitrary entity object (used by the 'default value' widget).
  $ids = (object) array(
    'entity_type' => $this->instance['entity_type'],
    'bundle' => $this->instance['bundle'],
    'entity_id' => NULL,
  );
  $form['#entity'] = _field_create_entity_from_ids($ids);
  $form['#entity']->field_ui_default_value = TRUE;
  if (!empty($field['locked'])) {
    $form['locked'] = array(
      '#markup' => t('The field %field is locked and cannot be edited.', array(
        '%field' => $this->instance
          ->label(),
      )),
    );
    return $form;
  }

  // Create a form structure for the instance values.
  $form['instance'] = array(
    '#tree' => TRUE,
  );

  // Build the non-configurable instance values.
  $form['instance']['field_name'] = array(
    '#type' => 'value',
    '#value' => $this->instance['field_name'],
  );
  $form['instance']['entity_type'] = array(
    '#type' => 'value',
    '#value' => $entity_type,
  );
  $form['instance']['bundle'] = array(
    '#type' => 'value',
    '#value' => $bundle,
  );

  // Build the configurable instance values.
  $form['instance']['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#default_value' => $this->instance
      ->label() ?: $field['field_name'],
    '#required' => TRUE,
    '#weight' => -20,
  );
  $form['instance']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Help text'),
    '#default_value' => !empty($this->instance['description']) ? $this->instance['description'] : '',
    '#rows' => 5,
    '#description' => t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array(
      '@tags' => _field_filter_xss_display_allowed_tags(),
    )) . '<br />' . t('This field supports tokens.'),
    '#weight' => -10,
  );
  $form['instance']['required'] = array(
    '#type' => 'checkbox',
    '#title' => t('Required field'),
    '#default_value' => !empty($this->instance['required']),
    '#weight' => -5,
  );

  // Add additional field instance settings from the field module.
  $additions = \Drupal::moduleHandler()
    ->invoke($field['module'], 'field_instance_settings_form', array(
    $field,
    $this->instance,
    $form_state,
  ));
  if (is_array($additions)) {
    $form['instance']['settings'] = $additions;
    $form['instance']['settings']['#weight'] = 10;
  }

  // Add widget settings for the widget type.
  $additions = $entity_form_display
    ->getWidget($this->instance
    ->getField()->id)
    ->settingsForm($form, $form_state);
  $form['instance']['widget']['settings'] = $additions ?: array(
    '#type' => 'value',
    '#value' => array(),
  );
  $form['instance']['widget']['#weight'] = 20;

  // Add handling for default value if not provided by any other module.
  if (field_behaviors_widget('default_value', $this->instance) == FIELD_BEHAVIOR_DEFAULT && empty($this->instance['default_value_function'])) {
    $form['instance']['default_value_widget'] = $this
      ->getDefaultValueWidget($field, $form, $form_state);
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
  );
  $form['actions']['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete field'),
    '#submit' => array(
      array(
        $this,
        'delete',
      ),
    ),
  );
  return $form;
}