/**
  * {@inheritdoc}
  */
 public function calculateDependencies()
 {
     $this->dependencies = parent::calculateDependencies();
     // Add a dependency on the module that provides the field type using the
     // source plugin configuration.
     $source_configuration = $this->migration->get('source');
     if (isset($source_configuration['constants']['type'])) {
         $field_type = $this->fieldTypePluginManager->getDefinition($source_configuration['constants']['type']);
         $this->addDependency('module', $field_type['provider']);
     }
     return $this->dependencies;
 }
 /**
  * Merges default values for widget configuration.
  *
  * @param string $field_type
  *   The field type.
  * @param array $configuration
  *   An array of widget configuration.
  *
  * @return array
  *   The display properties with defaults added.
  */
 public function prepareConfiguration($field_type, array $configuration)
 {
     // Fill in defaults for missing properties.
     $configuration += array('settings' => array(), 'third_party_settings' => array());
     // If no widget is specified, use the default widget.
     if (!isset($configuration['type'])) {
         $field_type = $this->fieldTypeManager->getDefinition($field_type);
         $configuration['type'] = $field_type['default_widget'];
     }
     // Fill in default settings values for the widget.
     $configuration['settings'] += $this->getDefaultSettings($configuration['type']);
     return $configuration;
 }
 /**
  * Merges default values for formatter configuration.
  *
  * @param string $field_type
  *   The field type.
  * @param array $configuration
  *   An array of formatter configuration.
  *
  * @return array
  *   The display properties with defaults added.
  */
 public function prepareConfiguration($field_type, array $configuration)
 {
     // Fill in defaults for missing properties.
     $configuration += array('label' => 'above', 'settings' => array(), 'third_party_settings' => array());
     // If no formatter is specified, use the default formatter.
     if (!isset($configuration['type'])) {
         $field_type = $this->fieldTypeManager->getDefinition($field_type);
         $configuration['type'] = $field_type['default_formatter'];
     }
     // Filter out unknown settings, and fill in defaults for missing settings.
     $default_settings = $this->getDefaultSettings($configuration['type']);
     $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
     return $configuration;
 }
Exemple #4
0
 /**
  * Gets field type definition.
  *
  * @return array
  *   The field type definition.
  */
 protected function getFieldTypeDefinition()
 {
     return $this->fieldTypeManager->getDefinition($this->getFieldStorageDefinition()->getType());
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  */
 protected function defineOptions()
 {
     $options = parent::defineOptions();
     $field_storage_definition = $this->getFieldStorageDefinition();
     $field_type = $this->fieldTypePluginManager->getDefinition($field_storage_definition->getType());
     $column_names = array_keys($field_storage_definition->getColumns());
     $default_column = '';
     // Try to determine a sensible default.
     if (count($column_names) == 1) {
         $default_column = $column_names[0];
     } elseif (in_array('value', $column_names)) {
         $default_column = 'value';
     }
     // If the field has a "value" column, we probably need that one.
     $options['click_sort_column'] = array('default' => $default_column);
     if (isset($this->definition['default_formatter'])) {
         $options['type'] = ['default' => $this->definition['default_formatter']];
     } elseif (isset($field_type['default_formatter'])) {
         $options['type'] = ['default' => $field_type['default_formatter']];
     } else {
         $options['type'] = ['default' => ''];
     }
     $options['settings'] = array('default' => isset($this->definition['default_formatter_settings']) ? $this->definition['default_formatter_settings'] : []);
     $options['group_column'] = array('default' => $default_column);
     $options['group_columns'] = array('default' => array());
     // Options used for multiple value fields.
     $options['group_rows'] = array('default' => TRUE);
     // If we know the exact number of allowed values, then that can be
     // the default. Otherwise, default to 'all'.
     $options['delta_limit'] = array('default' => $field_storage_definition->getCardinality() > 1 ? $field_storage_definition->getCardinality() : 0);
     $options['delta_offset'] = array('default' => 0);
     $options['delta_reversed'] = array('default' => FALSE);
     $options['delta_first_last'] = array('default' => FALSE);
     $options['multi_type'] = array('default' => 'separator');
     $options['separator'] = array('default' => ', ');
     $options['field_api_classes'] = array('default' => FALSE);
     return $options;
 }
 /**
  * {@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.'));
     }
 }