Esempio n. 1
0
 /**
  * @covers ::getNextDestination
  */
 public function testGetNextDestinationRouteName()
 {
     $destinations = [['route_name' => 'system.admin'], ['route_name' => 'system.admin_content']];
     $expected_route_name = 'system.admin';
     $expected_query = ['destinations' => [['route_name' => 'system.admin_content']]];
     $actual = FieldUI::getNextDestination($destinations);
     $this->assertSame($expected_route_name, $actual->getRouteName());
     $this->assertSame($expected_query, $actual->getOption('query'));
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $form_values = $form_state->getValues();
     $field_values = $form_values['field_storage'];
     // 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->field->getFieldStorageDefinition();
     foreach ($field_values as $key => $value) {
         $field_storage->set($key, $value);
     }
     // Update the field.
     try {
         $field_storage->save();
         drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->field->label())));
         $request = $this->getRequest();
         if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
             $request->query->remove('destinations');
             $form_state->setRedirectUrl($next_destination);
         } else {
             $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->field->entity_type, $this->field->bundle));
         }
     } catch (\Exception $e) {
         drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $this->field->label(), '%message' => $e->getMessage())), 'error');
     }
 }
Esempio n. 3
0
 /**
  * Overrides \Drupal\field_ui\OverviewBase::submitForm().
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $error = FALSE;
     $form_values = $form_state['values']['fields'];
     $destinations = array();
     // Create new field.
     if (!empty($form_values['_add_new_field']['field_name'])) {
         $values = $form_values['_add_new_field'];
         $field_storage = array('name' => $values['field_name'], 'entity_type' => $this->entity_type, 'type' => $values['type'], 'translatable' => $values['translatable']);
         $instance = array('field_name' => $values['field_name'], 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label'], 'translatable' => FALSE);
         // Create the field and instance.
         try {
             $this->entityManager->getStorage('field_storage_config')->create($field_storage)->save();
             $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance);
             $new_instance->save();
             // Make sure the field is displayed in the 'default' form mode (using
             // default widget and settings). It stays hidden for other form modes
             // until it is explicitly configured.
             entity_get_form_display($this->entity_type, $this->bundle, 'default')->setComponent($values['field_name'])->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->entity_type, $this->bundle, 'default')->setComponent($values['field_name'])->save();
             // Always show the field settings step, as the cardinality needs to be
             // configured for new fields.
             $route_parameters = array($this->bundleEntityType => $this->bundle, 'field_instance_config' => $new_instance->id());
             $destinations[] = array('route_name' => 'field_ui.storage_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
             $destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
             // Store new field information for any additional submit handlers.
             $form_state['fields_added']['_add_new_field'] = $values['field_name'];
         } catch (\Exception $e) {
             $error = TRUE;
             drupal_set_message($this->t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
         }
     }
     // Re-use existing field.
     if (!empty($form_values['_add_existing_field']['field_name'])) {
         $values = $form_values['_add_existing_field'];
         $field_name = $values['field_name'];
         $field_storage = FieldStorageConfig::loadByName($this->entity_type, $field_name);
         if (!empty($field_storage->locked)) {
             drupal_set_message($this->t('The field %label cannot be added because it is locked.', array('%label' => $values['label'])), 'error');
         } else {
             $instance = array('field_name' => $field_name, 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label']);
             try {
                 $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance);
                 $new_instance->save();
                 // Make sure the field is displayed in the 'default' form mode (using
                 // default widget and settings). It stays hidden for other form modes
                 // until it is explicitly configured.
                 entity_get_form_display($this->entity_type, $this->bundle, 'default')->setComponent($field_name)->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->entity_type, $this->bundle, 'default')->setComponent($field_name)->save();
                 $destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => array($this->bundleEntityType => $this->bundle, 'field_instance_config' => $new_instance->id()));
                 // Store new field information for any additional submit handlers.
                 $form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
             } catch (\Exception $e) {
                 $error = TRUE;
                 drupal_set_message($this->t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error');
             }
         }
     }
     if ($destinations) {
         $destination = drupal_get_destination();
         $destinations[] = $destination['destination'];
         $next_destination = FieldUI::getNextDestination($destinations, $form_state);
         if (isset($next_destination['route_name'])) {
             $form_state->setRedirect($next_destination['route_name'], $next_destination['route_parameters'], $next_destination['options']);
         } else {
             $form_state['redirect'] = $next_destination;
         }
     } elseif (!$error) {
         drupal_set_message($this->t('Your settings have been saved.'));
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function save(array $form, FormStateInterface $form_state)
 {
     $this->entity->save();
     drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->entity->getLabel())));
     $request = $this->getRequest();
     if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
         $request->query->remove('destinations');
         $form_state->setRedirectUrl($next_destination);
     } else {
         $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->bundle));
     }
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $error = FALSE;
     $values = $form_state->getValues();
     $destinations = array();
     $entity_type = $this->entityManager->getDefinition($this->entityTypeId);
     // Create new field.
     if ($values['new_storage_type']) {
         $field_storage_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'type' => $values['new_storage_type'], 'translatable' => $values['translatable']];
         $field_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['label'], 'translatable' => FALSE];
         $widget_id = $formatter_id = NULL;
         // Check if we're dealing with a preconfigured field.
         if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) {
             list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3);
             $field_storage_values['type'] = $field_type;
             $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class'];
             $field_options = $field_type_class::getPreconfiguredOptions()[$option_key];
             // Merge in preconfigured field storage options.
             if (isset($field_options['field_storage_config'])) {
                 foreach (array('cardinality', 'settings') as $key) {
                     if (isset($field_options['field_storage_config'][$key])) {
                         $field_storage_values[$key] = $field_options['field_storage_config'][$key];
                     }
                 }
             }
             // Merge in preconfigured field options.
             if (isset($field_options['field_config'])) {
                 foreach (array('required', 'settings') as $key) {
                     if (isset($field_options['field_config'][$key])) {
                         $field_values[$key] = $field_options['field_config'][$key];
                     }
                 }
             }
             $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL;
             $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL;
         }
         // Create the field storage and field.
         try {
             $this->entityManager->getStorage('field_storage_config')->create($field_storage_values)->save();
             $field = $this->entityManager->getStorage('field_config')->create($field_values);
             $field->save();
             $this->configureEntityFormDisplay($values['field_name'], $widget_id);
             $this->configureEntityViewDisplay($values['field_name'], $formatter_id);
             // Always show the field settings step, as the cardinality needs to be
             // configured for new fields.
             $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
             $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form", 'route_parameters' => $route_parameters);
             $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters);
             $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters);
             // Store new field information for any additional submit handlers.
             $form_state->set(['fields_added', '_add_new_field'], $values['field_name']);
         } catch (\Exception $e) {
             $error = TRUE;
             drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error');
         }
     }
     // Re-use existing field.
     if ($values['existing_storage_name']) {
         $field_name = $values['existing_storage_name'];
         try {
             $field = $this->entityManager->getStorage('field_config')->create(array('field_name' => $field_name, 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['existing_storage_label']));
             $field->save();
             $this->configureEntityFormDisplay($field_name);
             $this->configureEntityViewDisplay($field_name);
             $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
             $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters);
             $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters);
             // Store new field information for any additional submit handlers.
             $form_state->set(['fields_added', '_add_existing_field'], $field_name);
         } catch (\Exception $e) {
             $error = TRUE;
             drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error');
         }
     }
     if ($destinations) {
         $destination = $this->getDestinationArray();
         $destinations[] = $destination['destination'];
         $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
     } elseif (!$error) {
         drupal_set_message($this->t('Your settings have been saved.'));
     }
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Handle the default value.
     $default_value = array();
     if (isset($form['field']['default_value'])) {
         $items = $form['#entity']->get($this->field->getName());
         $default_value = $items->defaultValuesFormSubmit($form['field']['default_value'], $form, $form_state);
     }
     $this->field->default_value = $default_value;
     // Merge incoming values into the field.
     foreach ($form_state->getValue('field') as $key => $value) {
         $this->field->set($key, $value);
     }
     $this->field->save();
     drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->field->getLabel())));
     $request = $this->getRequest();
     if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
         $request->query->remove('destinations');
         $form_state->setRedirectUrl($next_destination);
     } else {
         $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->field->entity_type, $this->field->bundle));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function save(array $form, FormStateInterface $form_state)
 {
     $field_label = $form_state->get('field_config')->label();
     try {
         $this->entity->save();
         drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $field_label)));
         $request = $this->getRequest();
         if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
             $request->query->remove('destinations');
             $form_state->setRedirectUrl($next_destination);
         } else {
             $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($form_state->get('entity_type_id'), $form_state->get('bundle')));
         }
     } catch (\Exception $e) {
         drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $field_label, '%message' => $e->getMessage())), 'error');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     // Handle the default value.
     $default_value = array();
     if (isset($form['instance']['default_value'])) {
         $items = $form['#entity']->get($this->instance->getName());
         $default_value = $items->defaultValuesFormSubmit($form['instance']['default_value'], $form, $form_state);
     }
     $this->instance->default_value = $default_value;
     // Merge incoming values into the instance.
     foreach ($form_state['values']['instance'] as $key => $value) {
         $this->instance->{$key} = $value;
     }
     $this->instance->save();
     drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->instance->getLabel())));
     $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);
     }
 }