FieldOverview.php 20.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field_ui\FieldOverview.
6 7 8 9
 */

namespace Drupal\field_ui;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Entity\EntityListBuilderInterface;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Extension\ModuleHandlerInterface;
14
use Drupal\Core\Field\FieldTypePluginManagerInterface;
15
use Drupal\Core\Form\FormStateInterface;
16
use Drupal\Core\Render\Element;
17
use Drupal\field_ui\OverviewBase;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Drupal\field\Entity\FieldStorageConfig;
20
use Drupal\field\FieldInstanceConfigInterface;
21 22 23 24 25 26

/**
 * Field UI field overview form.
 */
class FieldOverview extends OverviewBase {

27 28 29
  /**
   *  The field type manager.
   *
30
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
31 32 33 34 35 36
   */
  protected $fieldTypeManager;

  /**
   * Constructs a new FieldOverview.
   *
37
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38
   *   The entity manager.
39
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
40 41
   *   The field type manager
   */
42
  public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) {
43
    parent::__construct($entity_manager);
44 45 46 47 48 49 50 51
    $this->fieldTypeManager = $field_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
52
      $container->get('entity.manager'),
53
      $container->get('plugin.manager.field.field_type')
54 55 56
    );
  }

57 58 59
  /**
   * {@inheritdoc}
   */
60 61 62
  public function getRegions() {
    return array(
      'content' => array(
63
        'title' => $this->t('Content'),
64
        'invisible' => TRUE,
65
        // @todo Bring back this message in https://drupal.org/node/1963340.
66
        //'message' => $this->t('No fields are present yet.'),
67 68 69 70 71
      ),
    );
  }

  /**
72
   * {@inheritdoc}
73
   */
74
  public function getFormId() {
75 76
    return 'field_ui_field_overview_form';
  }
77

78
  /**
79
   * {@inheritdoc}
80
   */
81
  public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
82
    parent::buildForm($form, $form_state, $entity_type_id, $bundle);
83

84
    // Gather bundle information.
85 86 87
    $instances = array_filter(\Drupal::entityManager()->getFieldDefinitions($this->entity_type, $this->bundle), function ($field_definition) {
      return $field_definition instanceof FieldInstanceConfigInterface;
    });
88
    $field_types = $this->fieldTypeManager->getDefinitions();
89

90
    // Field prefix.
91
    $field_prefix = \Drupal::config('field_ui.settings')->get('field_prefix');
92

93 94 95 96 97 98 99 100 101 102
    $form += array(
      '#entity_type' => $this->entity_type,
      '#bundle' => $this->bundle,
      '#fields' => array_keys($instances),
    );

    $table = array(
      '#type' => 'field_ui_table',
      '#tree' => TRUE,
      '#header' => array(
103
        $this->t('Label'),
104 105 106 107
        array(
          'data' => $this->t('Machine name'),
          'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
        ),
108 109
        $this->t('Field type'),
        $this->t('Operations'),
110 111 112 113 114 115 116 117 118 119
      ),
      '#regions' => $this->getRegions(),
      '#attributes' => array(
        'class' => array('field-ui-overview'),
        'id' => 'field-overview',
      ),
    );

    // Fields.
    foreach ($instances as $name => $instance) {
120
      $field_storage = $instance->getFieldStorageDefinition();
121 122
      $route_parameters = array(
        $this->bundleEntityType => $this->bundle,
123
        'field_instance_config' => $instance->id(),
124
      );
125
      $table[$name] = array(
126 127 128
        '#attributes' => array(
          'id' => drupal_html_class($name),
        ),
129
        'label' => array(
130
          '#markup' => String::checkPlain($instance->getLabel()),
131 132
        ),
        'field_name' => array(
133
          '#markup' => $instance->getName(),
134 135 136
        ),
        'type' => array(
          '#type' => 'link',
137 138
          '#title' => $field_types[$field_storage->getType()]['label'],
          '#route_name' => 'field_ui.storage_edit_' . $this->entity_type,
139
          '#route_parameters' => $route_parameters,
140
          '#options' => array('attributes' => array('title' => $this->t('Edit field settings.'))),
141 142 143 144 145
        ),
      );

      $table[$name]['operations']['data'] = array(
        '#type' => 'operations',
146
        '#links' => $this->entityManager->getListBuilder('field_instance_config')->getOperations($instance),
147 148
      );

149
      if (!empty($field_storage->locked)) {
150
        $table[$name]['operations'] = array('#markup' => $this->t('Locked'));
151 152 153 154
        $table[$name]['#attributes']['class'][] = 'menu-disabled';
      }
    }

155 156 157
    // Gather valid field types.
    $field_type_options = array();
    foreach ($field_types as $name => $field_type) {
158 159
      // Skip field types which should not be added via user interface.
      if (empty($field_type['no_ui'])) {
160 161 162 163 164
        $field_type_options[$name] = $field_type['label'];
      }
    }
    asort($field_type_options);

165 166
    // Additional row: add new field.
    if ($field_type_options) {
167 168
      $name = '_add_new_field';
      $table[$name] = array(
169
        '#attributes' => array('class' => array('add-new')),
170 171
        'label' => array(
          '#type' => 'textfield',
172
          '#title' => $this->t('New field label'),
173 174
          '#title_display' => 'invisible',
          '#size' => 15,
175 176
          '#description' => $this->t('Label'),
          '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . $this->t('Add new field') .'</div>',
177 178 179 180
          '#suffix' => '</div>',
        ),
        'field_name' => array(
          '#type' => 'machine_name',
181
          '#title' => $this->t('New field name'),
182 183
          '#title_display' => 'invisible',
          // This field should stay LTR even for RTL languages.
184
          '#field_prefix' => '<span dir="ltr">' . $field_prefix,
185 186
          '#field_suffix' => '</span>&lrm;',
          '#size' => 15,
187
          '#description' => $this->t('A unique machine-readable name containing letters, numbers, and underscores.'),
188 189
          // Calculate characters depending on the length of the field prefix
          // setting. Maximum length is 32.
190
          '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
191 192 193
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
          '#machine_name' => array(
            'source' => array('fields', $name, 'label'),
194
            'exists' => array($this, 'fieldNameExists'),
195 196 197 198 199 200 201
            'standalone' => TRUE,
            'label' => '',
          ),
          '#required' => FALSE,
        ),
        'type' => array(
          '#type' => 'select',
202
          '#title' => $this->t('Type of new field'),
203 204
          '#title_display' => 'invisible',
          '#options' => $field_type_options,
205 206
          '#empty_option' => $this->t('- Select a field type -'),
          '#description' => $this->t('Type of data to store.'),
207
          '#attributes' => array('class' => array('field-type-select')),
208
          '#cell_attributes' => array('colspan' => 2),
209 210 211 212 213 214
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        ),
        // Place the 'translatable' property as an explicit value so that
        // contrib modules can form_alter() the value for newly created fields.
        'translatable' => array(
          '#type' => 'value',
215
          '#value' => TRUE,
216 217 218 219 220
        ),
      );
    }

    // Additional row: re-use existing field.
221
    $existing_fields = $this->getExistingFieldOptions();
222
    if ($existing_fields) {
223 224 225
      // Build list of options.
      $existing_field_options = array();
      foreach ($existing_fields as $field_name => $info) {
226
        $text = $this->t('@type: @field (@label)', array(
227 228 229 230 231 232 233 234 235
          '@type' => $info['type_label'],
          '@label' => $info['label'],
          '@field' => $info['field'],
        ));
        $existing_field_options[$field_name] = truncate_utf8($text, 80, FALSE, TRUE);
      }
      asort($existing_field_options);
      $name = '_add_existing_field';
      $table[$name] = array(
236
        '#attributes' => array('class' => array('add-new')),
237
        '#row_type' => 'add_new_field',
238
        '#region_callback' => array($this, 'getRowRegion'),
239 240
        'label' => array(
          '#type' => 'textfield',
241
          '#title' => $this->t('Existing field label'),
242 243
          '#title_display' => 'invisible',
          '#size' => 15,
244
          '#description' => $this->t('Label'),
245
          '#attributes' => array('class' => array('label-textfield')),
246
          '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . $this->t('Re-use existing field') .'</div>',
247 248 249 250
          '#suffix' => '</div>',
        ),
        'field_name' => array(
          '#type' => 'select',
251
          '#title' => $this->t('Existing field to share'),
252 253
          '#title_display' => 'invisible',
          '#options' => $existing_field_options,
254 255
          '#empty_option' => $this->t('- Select an existing field -'),
          '#description' => $this->t('Field to share'),
256 257 258 259 260 261 262
          '#attributes' => array('class' => array('field-select')),
          '#cell_attributes' => array('colspan' => 3),
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        ),
      );
    }

263 264 265
    // We can set the 'rows_order' element, needed by theme_field_ui_table(),
    // here instead of a #pre_render callback because this form doesn't have the
    // tabledrag behavior anymore.
266
    $table['#regions']['content']['rows_order'] = array();
267
    foreach (Element::children($table) as $name) {
268 269
      $table['#regions']['content']['rows_order'][] = $name;
    }
270

271
    $form['fields'] = $table;
272 273

    $form['actions'] = array('#type' => 'actions');
274 275 276 277
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this->t('Save'));
278 279 280 281 282

    return $form;
  }

  /**
283
   * {@inheritdoc}
284
   */
285
  public function validateForm(array &$form, FormStateInterface $form_state) {
286 287 288 289 290 291 292 293 294
    $this->validateAddNew($form, $form_state);
    $this->validateAddExisting($form, $form_state);
  }

  /**
   * Validates the 'add new field' row.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
295 296
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
297
   *
298
   * @see \Drupal\field_ui\FieldOverview::validateForm()
299
   */
300
  protected function validateAddNew(array $form, FormStateInterface $form_state) {
301
    $field = $form_state->getValue(array('fields', '_add_new_field'));
302 303

    // Validate if any information was provided in the 'add new field' row.
304
    if (array_filter(array($field['label'], $field['field_name'], $field['type']))) {
305 306
      // Missing label.
      if (!$field['label']) {
307
        $form_state->setErrorByName('fields][_add_new_field][label', $this->t('Add new field: you need to provide a label.'));
308 309 310 311
      }

      // Missing field name.
      if (!$field['field_name']) {
312
        $form_state->setErrorByName('fields][_add_new_field][field_name', $this->t('Add new field: you need to provide a machine name for the field.'));
313 314 315 316 317
      }
      // Field name validation.
      else {
        $field_name = $field['field_name'];

318
        // Add the field prefix.
319
        $field_name = \Drupal::config('field_ui.settings')->get('field_prefix') . $field_name;
320 321 322 323 324
        form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state);
      }

      // Missing field type.
      if (!$field['type']) {
325
        $form_state->setErrorByName('fields][_add_new_field][type', $this->t('Add new field: you need to select a field type.'));
326 327 328 329 330 331 332 333 334
      }
    }
  }

  /**
   * Validates the 're-use existing field' row.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
335 336
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
337
   *
338
   * @see \Drupal\field_ui\FieldOverview::validate()
339
   */
340
  protected function validateAddExisting(array $form, FormStateInterface $form_state) {
341 342
    // The form element might be absent if no existing fields can be added to
    // this bundle.
343
    if ($field = $form_state->getValue(array('fields', '_add_existing_field'))) {
344 345
      // Validate if any information was provided in the
      // 're-use existing field' row.
346
      if (array_filter(array($field['label'], $field['field_name']))) {
347 348
        // Missing label.
        if (!$field['label']) {
349
          $form_state->setErrorByName('fields][_add_existing_field][label', $this->t('Re-use existing field: you need to provide a label.'));
350 351 352 353
        }

        // Missing existing field name.
        if (!$field['field_name']) {
354
          $form_state->setErrorByName('fields][_add_existing_field][field_name', $this->t('Re-use existing field: you need to select a field.'));
355 356 357 358 359 360
        }
      }
    }
  }

  /**
361
   * Overrides \Drupal\field_ui\OverviewBase::submitForm().
362
   */
363
  public function submitForm(array &$form, FormStateInterface $form_state) {
364
    $error = FALSE;
365
    $form_values = $form_state->getValue('fields');
366 367 368 369 370 371
    $destinations = array();

    // Create new field.
    if (!empty($form_values['_add_new_field']['field_name'])) {
      $values = $form_values['_add_new_field'];

372
      $field_storage = array(
373 374
        'name' => $values['field_name'],
        'entity_type' => $this->entity_type,
375 376 377 378
        'type' => $values['type'],
        'translatable' => $values['translatable'],
      );
      $instance = array(
379
        'field_name' => $values['field_name'],
380 381 382
        'entity_type' => $this->entity_type,
        'bundle' => $this->bundle,
        'label' => $values['label'],
383 384
        // Field translatability should be explicitly enabled by the users.
        'translatable' => FALSE,
385 386 387 388
      );

      // Create the field and instance.
      try {
389
        $this->entityManager->getStorage('field_storage_config')->create($field_storage)->save();
390
        $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance);
391
        $new_instance->save();
392

393
        // Make sure the field is displayed in the 'default' form mode (using
394 395
        // default widget and settings). It stays hidden for other form modes
        // until it is explicitly configured.
396
        entity_get_form_display($this->entity_type, $this->bundle, 'default')
397
          ->setComponent($values['field_name'])
398 399
          ->save();

400 401 402 403
        // 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')
404
          ->setComponent($values['field_name'])
405 406
          ->save();

407 408
        // Always show the field settings step, as the cardinality needs to be
        // configured for new fields.
409 410
        $route_parameters = array(
          $this->bundleEntityType => $this->bundle,
411
          'field_instance_config' => $new_instance->id(),
412
        );
413
        $destinations[] = array('route_name' => 'field_ui.storage_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
414
        $destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => $route_parameters);
415 416

        // Store new field information for any additional submit handlers.
417
        $form_state['fields_added']['_add_new_field'] = $values['field_name'];
418
      }
419
      catch (\Exception $e) {
420 421
        $error = TRUE;
        drupal_set_message($this->t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
422 423 424 425 426 427
      }
    }

    // Re-use existing field.
    if (!empty($form_values['_add_existing_field']['field_name'])) {
      $values = $form_values['_add_existing_field'];
428
      $field_name = $values['field_name'];
429 430
      $field_storage = FieldStorageConfig::loadByName($this->entity_type, $field_name);
      if (!empty($field_storage->locked)) {
431
        drupal_set_message($this->t('The field %label cannot be added because it is locked.', array('%label' => $values['label'])), 'error');
432 433 434
      }
      else {
        $instance = array(
435
          'field_name' => $field_name,
436 437 438 439 440 441
          'entity_type' => $this->entity_type,
          'bundle' => $this->bundle,
          'label' => $values['label'],
        );

        try {
442
          $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance);
443
          $new_instance->save();
444

445
          // Make sure the field is displayed in the 'default' form mode (using
446 447
          // default widget and settings). It stays hidden for other form modes
          // until it is explicitly configured.
448
          entity_get_form_display($this->entity_type, $this->bundle, 'default')
449
            ->setComponent($field_name)
450 451
            ->save();

452 453 454 455
          // 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')
456
            ->setComponent($field_name)
457 458
            ->save();

459 460 461 462
          $destinations[] = array(
            'route_name' => 'field_ui.instance_edit_' . $this->entity_type,
            'route_parameters' => array(
              $this->bundleEntityType => $this->bundle,
463
              'field_instance_config' => $new_instance->id(),
464 465
            ),
          );
466 467 468
          // Store new field information for any additional submit handlers.
          $form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
        }
469
        catch (\Exception $e) {
470 471
          $error = TRUE;
          drupal_set_message($this->t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error');
472 473 474 475 476 477 478
        }
      }
    }

    if ($destinations) {
      $destination = drupal_get_destination();
      $destinations[] = $destination['destination'];
479
      $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
480
    }
481
    elseif (!$error) {
482
      drupal_set_message($this->t('Your settings have been saved.'));
483 484
    }
  }
485 486 487 488 489 490 491 492

  /**
   * Returns an array of existing fields to be added to a bundle.
   *
   * @return array
   *   An array of existing fields keyed by field name.
   */
  protected function getExistingFieldOptions() {
493 494 495 496
    $options = array();

    // Collect candidate field instances: all instances of fields for this
    // entity type that are not already present in the current bundle.
497
    $field_map = \Drupal::entityManager()->getFieldMap();
498 499 500 501 502 503 504 505 506 507 508 509 510
    $instance_ids = array();
    if (!empty($field_map[$this->entity_type])) {
      foreach ($field_map[$this->entity_type] as $field_name => $data) {
        if (!in_array($this->bundle, $data['bundles'])) {
          $bundle = reset($data['bundles']);
          $instance_ids[] = $this->entity_type . '.' . $bundle . '.' . $field_name;
        }
      }
    }

    // Load the instances and build the list of options.
    if ($instance_ids) {
      $field_types = $this->fieldTypeManager->getDefinitions();
511
      $instances = $this->entityManager->getStorage('field_instance_config')->loadMultiple($instance_ids);
512 513 514 515
      foreach ($instances as $instance) {
        // Do not show:
        // - locked fields,
        // - fields that should not be added via user interface.
516
        $field_type = $instance->getType();
517 518
        $field_storage = $instance->getFieldStorageDefinition();
        if (empty($field_storage->locked) && empty($field_types[$field_type]['no_ui'])) {
519
          $options[$instance->getName()] = array(
520 521
            'type' => $field_type,
            'type_label' => $field_types[$field_type]['label'],
522 523
            'field' => $instance->getName(),
            'label' => $instance->getLabel(),
524
          );
525 526 527
        }
      }
    }
528 529

    return $options;
530 531 532 533 534 535
  }

  /**
   * Checks if a field machine name is taken.
   *
   * @param string $value
536
   *   The machine name, not prefixed.
537 538 539 540 541
   *
   * @return bool
   *   Whether or not the field machine name is taken.
   */
  public function fieldNameExists($value) {
542 543
    // Add the field prefix.
    $field_name = \Drupal::config('field_ui.settings')->get('field_prefix') . $value;
544

545 546
    $field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($this->entity_type);
    return isset($field_storage_definitions[$field_name]);
547 548
  }

549
}