View source
<?php
namespace Drupal\field_ui\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldStorageAddForm extends FormBase {
protected $entityTypeId;
protected $bundle;
protected $entityManager;
protected $fieldTypePluginManager;
public $queryFactory;
protected $configFactory;
public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, QueryFactory $query_factory, ConfigFactoryInterface $config_factory) {
$this->entityManager = $entity_manager;
$this->fieldTypePluginManager = $field_type_plugin_manager;
$this->queryFactory = $query_factory;
$this->configFactory = $config_factory;
}
public function getFormId() {
return 'field_ui_field_storage_add_form';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity.query'), $container
->get('config.factory'));
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
if (!$form_state
->get('entity_type_id')) {
$form_state
->set('entity_type_id', $entity_type_id);
}
if (!$form_state
->get('bundle')) {
$form_state
->set('bundle', $bundle);
}
$this->entityTypeId = $form_state
->get('entity_type_id');
$this->bundle = $form_state
->get('bundle');
$field_type_options = array();
foreach ($this->fieldTypePluginManager
->getGroupedDefinitions($this->fieldTypePluginManager
->getUiDefinitions()) as $category => $field_types) {
foreach ($field_types as $name => $field_type) {
$field_type_options[$category][$name] = $field_type['label'];
}
}
$form['add'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'form--inline',
'clearfix',
),
),
);
$form['add']['new_storage_type'] = array(
'#type' => 'select',
'#title' => $this
->t('Add a new field'),
'#options' => $field_type_options,
'#empty_option' => $this
->t('- Select a field type -'),
);
if ($existing_field_storage_options = $this
->getExistingFieldStorageOptions()) {
$form['add']['separator'] = array(
'#type' => 'item',
'#markup' => $this
->t('or'),
);
$form['add']['existing_storage_name'] = array(
'#type' => 'select',
'#title' => $this
->t('Re-use an existing field'),
'#options' => $existing_field_storage_options,
'#empty_option' => $this
->t('- Select an existing field -'),
);
$form['#attached']['drupalSettings']['existingFieldLabels'] = $this
->getExistingFieldLabels(array_keys($existing_field_storage_options));
}
else {
$form['add']['existing_storage_name'] = array(
'#type' => 'value',
'#value' => FALSE,
);
}
$form['new_storage_wrapper'] = array(
'#type' => 'container',
'#states' => array(
'!visible' => array(
':input[name="new_storage_type"]' => array(
'value' => '',
),
),
),
);
$form['new_storage_wrapper']['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#size' => 15,
);
$field_prefix = $this
->config('field_ui.settings')
->get('field_prefix');
$form['new_storage_wrapper']['field_name'] = array(
'#type' => 'machine_name',
'#field_prefix' => '<span dir="ltr">' . $field_prefix,
'#field_suffix' => '</span>‎',
'#size' => 15,
'#description' => $this
->t('A unique machine-readable name containing letters, numbers, and underscores.'),
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
'#machine_name' => array(
'source' => array(
'new_storage_wrapper',
'label',
),
'exists' => array(
$this,
'fieldNameExists',
),
),
'#required' => FALSE,
);
if ($existing_field_storage_options) {
$form['existing_storage_label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#size' => 15,
'#states' => array(
'!visible' => array(
':input[name="existing_storage_name"]' => array(
'value' => '',
),
),
),
);
}
$form['translatable'] = array(
'#type' => 'value',
'#value' => TRUE,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#button_type' => 'primary',
);
$form['#attached']['library'][] = 'field_ui/drupal.field_ui';
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getValue('new_storage_type') && !$form_state
->getValue('existing_storage_name')) {
$form_state
->setErrorByName('new_storage_type', $this
->t('You need to select a field type or an existing field.'));
}
elseif ($form_state
->getValue('new_storage_type') && $form_state
->getValue('existing_storage_name')) {
$form_state
->setErrorByName('new_storage_type', $this
->t('Adding a new field and re-using an existing field at the same time is not allowed.'));
return;
}
$this
->validateAddNew($form, $form_state);
$this
->validateAddExisting($form, $form_state);
}
protected function validateAddNew(array $form, FormStateInterface $form_state) {
if ($form_state
->getValue('new_storage_type')) {
if (!$form_state
->getValue('label')) {
$form_state
->setErrorByName('label', $this
->t('Add new field: you need to provide a label.'));
}
if (!$form_state
->getValue('field_name')) {
$form_state
->setErrorByName('field_name', $this
->t('Add new field: you need to provide a machine name for the field.'));
}
else {
$field_name = $form_state
->getValue('field_name');
$field_name = $this->configFactory
->get('field_ui.settings')
->get('field_prefix') . $field_name;
$form_state
->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name);
}
}
}
protected function validateAddExisting(array $form, FormStateInterface $form_state) {
if ($form_state
->getValue('existing_storage_name')) {
if (!$form_state
->getValue('existing_storage_label')) {
$form_state
->setErrorByName('existing_storage_label', $this
->t('Re-use existing field: you need to provide a label.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$error = FALSE;
$values = $form_state
->getValues();
$destinations = array();
$entity_type = $this->entityManager
->getDefinition($this->entityTypeId);
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;
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];
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];
}
}
}
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;
}
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);
$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,
);
$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');
}
}
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,
);
$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.'));
}
}
protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
$options = $widget_id ? [
'type' => $widget_id,
] : [];
entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($field_name, $options)
->save();
}
protected function configureEntityViewDisplay($field_name, $formatter_id = NULL) {
$options = $formatter_id ? [
'type' => $formatter_id,
] : [];
entity_get_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($field_name, $options)
->save();
}
protected function getExistingFieldStorageOptions() {
$options = array();
$field_types = $this->fieldTypePluginManager
->getDefinitions();
foreach ($this->entityManager
->getFieldStorageDefinitions($this->entityTypeId) as $field_name => $field_storage) {
$field_type = $field_storage
->getType();
if ($field_storage instanceof FieldStorageConfigInterface && !$field_storage
->isLocked() && empty($field_types[$field_type]['no_ui']) && !in_array($this->bundle, $field_storage
->getBundles(), TRUE)) {
$options[$field_name] = $this
->t('@type: @field', array(
'@type' => $field_types[$field_type]['label'],
'@field' => $field_name,
));
}
}
asort($options);
return $options;
}
protected function getExistingFieldLabels(array $field_names) {
$field_ids = $this->queryFactory
->get('field_config')
->condition('entity_type', $this->entityTypeId)
->condition('field_name', $field_names)
->execute();
$fields = $this->entityManager
->getStorage('field_config')
->loadMultiple($field_ids);
$labels = array();
foreach ($fields as $field) {
if (!isset($labels[$field
->getName()])) {
$labels[$field
->getName()] = $field
->label();
}
}
$labels += array_combine($field_names, $field_names);
return $labels;
}
public function fieldNameExists($value, $element, FormStateInterface $form_state) {
if ($form_state
->getValue('existing_storage_name')) {
return FALSE;
}
$field_name = $this->configFactory
->get('field_ui.settings')
->get('field_prefix') . $value;
$field_storage_definitions = $this->entityManager
->getFieldStorageDefinitions($this->entityTypeId);
return isset($field_storage_definitions[$field_name]);
}
}