/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, FieldInstanceConfigInterface $field_instance_config = NULL)
 {
     $this->instance = $form_state['instance'] = $field_instance_config;
     $bundle = $this->instance->bundle;
     $entity_type = $this->instance->entity_type;
     $field_storage = $this->instance->getFieldStorageDefinition();
     $bundles = entity_get_bundles();
     $form_title = $this->t('%instance settings for %bundle', array('%instance' => $this->instance->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->instance->entity_type, 'bundle' => $this->instance->bundle, 'entity_id' => NULL);
     $form['#entity'] = _field_create_entity_from_ids($ids);
     $items = $form['#entity']->get($this->instance->getName());
     if (!empty($field_storage->locked)) {
         $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->instance->getLabel())));
         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->getName());
     $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' => $this->t('Label'), '#default_value' => $this->instance->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20);
     $form['instance']['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->instance->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' => _field_filter_xss_display_allowed_tags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10);
     $form['instance']['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->instance->isRequired(), '#weight' => -5);
     // Add instance settings for the field type.
     $form['instance']['settings'] = $items[0]->instanceSettingsForm($form, $form_state);
     $form['instance']['settings']['#weight'] = 10;
     // Add handling for default value.
     if ($element = $items->defaultValuesForm($form, $form_state)) {
         $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['instance']['default_value'] = $element;
     }
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save settings'));
     $form['actions']['delete'] = array('#type' => 'submit', '#value' => $this->t('Delete field'), '#submit' => array(array($this, 'delete')));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     $form_values = $form_state['values'];
     $field_values = $form_values['field'];
     // Save field cardinality.
     $cardinality = $field_values['cardinality'];
     $cardinality_number = $field_values['cardinality_number'];
     if ($cardinality === 'number') {
         $cardinality = $cardinality_number;
     }
     $field_values['cardinality'] = $cardinality;
     unset($field_values['container']);
     // Merge incoming form values into the existing field.
     $field_storage = $this->instance->getFieldStorageDefinition();
     foreach ($field_values as $key => $value) {
         $field_storage->{$key} = $value;
     }
     // Update the field.
     try {
         $field_storage->save();
         drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->instance->label())));
         $request = $this->getRequest();
         if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
             $request->query->remove('destinations');
             if (isset($next_destination['route_name'])) {
                 $form_state['redirect_route'] = $next_destination;
             } else {
                 $form_state['redirect'] = $next_destination;
             }
         } else {
             $form_state['redirect_route'] = FieldUI::getOverviewRouteInfo($this->instance->entity_type, $this->instance->bundle);
         }
     } catch (\Exception $e) {
         drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $this->instance->label(), '%message' => $e->getMessage())), 'error');
     }
 }