EntityFormController.php 18.6 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\EntityFormController.
6 7
 */

8
namespace Drupal\Core\Entity;
9

10
use Drupal\entity\EntityFormDisplayInterface;
11
use Drupal\Core\Extension\ModuleHandlerInterface;
12
use Drupal\Core\Language\Language;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * Base class for entity form controllers.
 */
class EntityFormController implements EntityFormControllerInterface {

  /**
   * The name of the current operation.
   *
   * Subclasses may use this to implement different behaviors depending on its
   * value.
   *
   * @var string
   */
  protected $operation;

30 31 32 33 34 35 36
  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

37 38 39 40 41 42 43
  /**
   * The entity being used by this form.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

44
  /**
45 46
   * Constructs an EntityFormController object.
   *
47 48 49 50 51 52 53 54 55
   * @param \Drupal\Core\Extension\ModuleHandlerInterface
   *   The module handler service.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
56
   */
57 58 59 60
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static(
      $container->get('module_handler')
    );
61 62 63 64 65
  }

  /**
   * Sets the operation for this form.
   *
66 67
   * @param string $operation
   *   The name of the current operation.
68
   */
69 70 71 72 73
  public function setOperation($operation) {
    // If NULL is passed, do not overwrite the operation.
    if ($operation) {
      $this->operation = $operation;
    }
74 75 76
  }

  /**
77
   * {@inheritdoc}
78
   */
79
  public function getBaseFormID() {
80 81 82 83 84 85 86 87 88
    // Assign ENTITYTYPE_form as base form ID to invoke corresponding
    // hook_form_alter(), #validate, #submit, and #theme callbacks, but only if
    // it is different from the actual form ID, since callbacks would be invoked
    // twice otherwise.
    $base_form_id = $this->entity->entityType() . '_form';
    if ($base_form_id == $this->getFormID()) {
      $base_form_id = '';
    }
    return $base_form_id;
89
  }
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    $entity_type = $this->entity->entityType();
    $bundle = $this->entity->bundle();
    $form_id = $entity_type;
    if ($bundle != $entity_type) {
      $form_id = $bundle . '_' . $form_id;
    }
    if ($this->operation != 'default') {
      $form_id = $form_id . '_' . $this->operation;
    }
    return $form_id . '_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    // During the initial form build, add this controller to the form state and
    // allow for initial preparation before form building and processing.
    if (!isset($form_state['controller'])) {
      $this->init($form_state);
115 116 117
    }

    // Retrieve the form array using the possibly updated entity in form state.
118
    $form = $this->form($form, $form_state);
119 120 121 122 123 124 125 126 127 128

    // Retrieve and add the form actions array.
    $actions = $this->actionsElement($form, $form_state);
    if (!empty($actions)) {
      $form['actions'] = $actions;
    }

    return $form;
  }

129 130 131 132 133 134 135 136 137 138 139 140
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
  }

141 142 143
  /**
   * Initialize the form state and the entity before the first form build.
   */
144
  protected function init(array &$form_state) {
145 146 147
    // Add the controller to the form state so it can be easily accessed by
    // module-provided form handlers there.
    $form_state['controller'] = $this;
148

149 150 151 152 153
    // Ensure we act on the translation object corresponding to the current form
    // language.
    $this->entity = $this->getTranslatedEntity($form_state);

    // Prepare the entity to be presented in the entity form.
154
    $this->prepareEntity();
155

156
    $form_display = entity_get_render_form_display($this->entity, $this->getOperation());
157 158 159 160 161

    // Let modules alter the form display.
    $form_display_context = array(
      'entity_type' => $this->entity->entityType(),
      'bundle' => $this->entity->bundle(),
162
      'form_mode' => $this->getOperation(),
163
    );
164
    $this->moduleHandler->alter('entity_form_display', $form_display, $form_display_context);
165 166

    $this->setFormDisplay($form_display, $form_state);
167 168 169 170

    // Invoke the prepare form hooks.
    $this->prepareInvokeAll('entity_prepare_form', $form_state);
    $this->prepareInvokeAll($this->entity->entityType() . '_prepare_form', $form_state);
171 172 173 174 175
  }

  /**
   * Returns the actual form array to be built.
   *
176
   * @see Drupal\Core\Entity\EntityFormController::build()
177
   */
178 179
  public function form(array $form, array &$form_state) {
    $entity = $this->entity;
180
    // @todo Exploit the Field API to generate the default widgets for the
181 182 183
    // entity properties.
    $info = $entity->entityInfo();
    if (!empty($info['fieldable'])) {
184
      field_attach_form($entity, $form, $form_state, $this->getFormLangcode($form_state));
185
    }
186

187 188
    // Add a process callback so we can assign weights and hide extra fields.
    $form['#process'][] = array($this, 'processForm');
189

190 191 192 193 194 195
    if (!isset($form['langcode'])) {
      // If the form did not specify otherwise, default to keeping the existing
      // language of the entity or defaulting to the site default language for
      // new entities.
      $form['langcode'] = array(
        '#type' => 'value',
196
        '#value' => !$entity->isNew() ? $entity->getUntranslated()->language()->id : language_default()->id,
197 198
      );
    }
199 200 201
    return $form;
  }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  /**
   * Process callback: assigns weights and hides extra fields.
   *
   * @see \Drupal\Core\Entity\EntityFormController::form()
   */
  public function processForm($element, $form_state, $form) {
    // Assign the weights configured in the form display.
    foreach ($this->getFormDisplay($form_state)->getComponents() as $name => $options) {
      if (isset($element[$name])) {
        $element[$name]['#weight'] = $options['weight'];
      }
    }

    // Hide extra fields.
    $extra_fields = field_info_extra_fields($this->entity->entityType(), $this->entity->bundle(), 'form');
    foreach ($extra_fields as $extra_field => $info) {
      if (!$this->getFormDisplay($form_state)->getComponent($extra_field)) {
        $element[$extra_field]['#access'] = FALSE;
      }
    }

    return $element;
  }

226 227 228 229 230 231 232
  /**
   * Returns the action form element for the current entity form.
   */
  protected function actionsElement(array $form, array &$form_state) {
    $element = $this->actions($form, $form_state);

    // We cannot delete an entity that has not been created yet.
233
    if ($this->entity->isNew()) {
234 235 236 237 238 239 240 241
      unset($element['delete']);
    }
    elseif (isset($element['delete'])) {
      // Move the delete action as last one, unless weights are explicitly
      // provided.
      $delete = $element['delete'];
      unset($element['delete']);
      $element['delete'] = $delete;
242 243 244 245 246 247
      $element['delete']['#button_type'] = 'danger';
    }

    if (isset($element['submit'])) {
      // Give the primary submit button a #button_type of primary.
      $element['submit']['#button_type'] = 'primary';
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    }

    $count = 0;
    foreach (element_children($element) as $action) {
      $element[$action] += array(
        '#type' => 'submit',
        '#weight' => ++$count * 5,
      );
    }

    if (!empty($element)) {
      $element['#type'] = 'actions';
    }

    return $element;
  }

  /**
   * Returns an array of supported actions for the current entity form.
   */
  protected function actions(array $form, array &$form_state) {
    return array(
      // @todo Rename the action key from submit to save.
      'submit' => array(
        '#value' => t('Save'),
        '#validate' => array(
          array($this, 'validate'),
        ),
        '#submit' => array(
          array($this, 'submit'),
          array($this, 'save'),
        ),
      ),
      'delete' => array(
        '#value' => t('Delete'),
        // No need to validate the form when deleting the entity.
        '#submit' => array(
          array($this, 'delete'),
        ),
      ),
      // @todo Consider introducing a 'preview' action here, since it is used by
      // many entity types.
    );
  }

  /**
294
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::validate().
295 296 297
   */
  public function validate(array $form, array &$form_state) {
    $entity = $this->buildEntity($form, $form_state);
298
    $entity_langcode = $entity->language()->id;
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    $violations = array();

    // @todo Simplify when all entity types are converted to EntityNG.
    if ($entity instanceof EntityNG) {
      foreach ($entity as $field_name => $field) {
        $field_violations = $field->validate();
        if (count($field_violations)) {
          $violations[$field_name] = $field_violations;
        }
      }
    }
    else {
      // For BC entities, iterate through each field instance and
      // instantiate NG items objects manually.
      $definitions = \Drupal::entityManager()->getFieldDefinitions($entity->entityType(), $entity->bundle());
      foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $field_name => $instance) {
        $langcode = field_is_translatable($entity->entityType(), $instance->getField()) ? $entity_langcode : Language::LANGCODE_NOT_SPECIFIED;

        // Create the field object.
        $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
        // @todo Exception : calls setValue(), tries to set the 'formatted'
        // property.
        $field = \Drupal::typedData()->create($definitions[$field_name], $items, $field_name, $entity);
        $field_violations = $field->validate();
        if (count($field_violations)) {
          $violations[$field->getName()] = $field_violations;
        }
      }
    }

    // Map errors back to form elements.
    if ($violations) {
      foreach ($violations as $field_name => $field_violations) {
        $langcode = field_is_translatable($entity->entityType(), field_info_field($field_name)) ? $entity_langcode : Language::LANGCODE_NOT_SPECIFIED;
        $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
        $field_state['constraint_violations'] = $field_violations;
        field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
      }

      field_invoke_method('flagErrors', _field_invoke_widget_target($form_state['form_display']), $entity, $form, $form_state);
340 341 342 343 344 345 346 347 348
    }

    // @todo Remove this.
    // Execute legacy global validation handlers.
    unset($form_state['validate_handlers']);
    form_execute_handlers('validate', $form, $form_state);
  }

  /**
349
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::submit().
350 351 352 353 354 355 356 357 358 359 360 361 362
   *
   * This is the default entity object builder function. It is called before any
   * other submit handler to build the new entity object to be passed to the
   * following submit handlers. At this point of the form workflow the entity is
   * validated and the form state can be updated, this way the subsequently
   * invoked handlers can retrieve a regular entity object to act on.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function submit(array $form, array &$form_state) {
363 364 365
    // Remove button and internal Form API values from submitted values.
    form_state_values_clean($form_state);

366
    $this->updateFormLangcode($form_state);
367
    $this->submitEntityLanguage($form, $form_state);
368 369
    $this->entity = $this->buildEntity($form, $form_state);
    return $this->entity;
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  }

  /**
   * Form submission handler for the 'save' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function save(array $form, array &$form_state) {
    // @todo Perform common save operations.
  }

  /**
   * Form submission handler for the 'delete' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function delete(array $form, array &$form_state) {
    // @todo Perform common delete operations.
  }

  /**
397
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode().
398
   */
399
  public function getFormLangcode(array $form_state) {
400
    $entity = $this->entity;
401 402 403 404 405 406 407 408

    if (!empty($form_state['langcode'])) {
      $langcode = $form_state['langcode'];
    }
    else {
      // If no form langcode was provided we default to the current content
      // language and inspect existing translations to find a valid fallback,
      // if any.
409
      $translations = $entity->getTranslationLanguages();
410
      $langcode = language(Language::TYPE_CONTENT)->id;
411 412 413 414 415 416 417 418
      $fallback = language_multilingual() ? language_fallback_get_candidates() : array();
      while (!empty($langcode) && !isset($translations[$langcode])) {
        $langcode = array_shift($fallback);
      }
    }

    // If the site is not multilingual or no translation for the given form
    // language is available, fall back to the entity language.
419
    return !empty($langcode) ? $langcode : $entity->getUntranslated()->language()->id;
420 421
  }

422
  /**
423
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::isDefaultFormLangcode().
424
   */
425
  public function isDefaultFormLangcode(array $form_state) {
426
    return $this->getFormLangcode($form_state) == $this->entity->getUntranslated()->language()->id;
427 428 429
  }

  /**
430
   * Updates the form language to reflect any change to the entity language.
431 432
   *
   * @param array $form_state
433
   *   A keyed array containing the current state of the form.
434
   */
435
  protected function updateFormLangcode(array $form_state) {
436 437 438 439
    // Update the form language as it might have changed.
    if (isset($form_state['values']['langcode']) && $this->isDefaultFormLangcode($form_state)) {
      $form_state['langcode'] = $form_state['values']['langcode'];
    }
440
  }
441

442 443 444 445 446 447 448 449 450
  /**
   * Handle possible entity language changes.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  protected function submitEntityLanguage(array $form, array &$form_state) {
451
    $entity = $this->entity;
452 453 454 455 456 457 458
    $entity_type = $entity->entityType();

    if (field_has_translation_handler($entity_type)) {
      // If we are editing the default language values, we use the submitted
      // entity language as the new language for fields to handle any language
      // change. Otherwise the current form language is the proper value, since
      // in this case it is not supposed to change.
459
      $current_langcode = $this->isDefaultFormLangcode($form_state) ? $form_state['values']['langcode'] : $this->getFormLangcode($form_state);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

      foreach (field_info_instances($entity_type, $entity->bundle()) as $instance) {
        $field_name = $instance['field_name'];
        $field = field_info_field($field_name);
        $previous_langcode = $form[$field_name]['#language'];

        // Handle a possible language change: new language values are inserted,
        // previous ones are deleted.
        if ($field['translatable'] && $previous_langcode != $current_langcode) {
          $form_state['values'][$field_name][$current_langcode] = $form_state['values'][$field_name][$previous_langcode];
          $form_state['values'][$field_name][$previous_langcode] = array();
        }
      }
    }
  }

476
  /**
477
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::buildEntity().
478 479
   */
  public function buildEntity(array $form, array &$form_state) {
480
    $entity = clone $this->entity;
481 482 483 484
    // If you submit a form, the form state comes from caching, which forces
    // the controller to be the one before caching. Ensure to have the
    // controller of the current request.
    $form_state['controller'] = $this;
485
    // @todo Move entity_form_submit_build_entity() here.
486
    // @todo Exploit the Field API to process the submitted entity field.
487 488 489 490 491
    entity_form_submit_build_entity($entity->entityType(), $entity, $form, $form_state);
    return $entity;
  }

  /**
492
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::getEntity().
493
   */
494 495
  public function getEntity() {
    return $this->entity;
496 497
  }

498 499 500 501 502 503 504 505 506 507 508 509 510
  /**
   * Returns the translation object corresponding to the form language.
   *
   * @param array $form_state
   *   A keyed array containing the current state of the form.
   */
  protected function getTranslatedEntity(array $form_state) {
    $langcode = $this->getFormLangcode($form_state);
    $translation = $this->entity->getTranslation($langcode);
    // Ensure that the entity object is a BC entity if the original one is.
    return $this->entity instanceof EntityBCDecorator ? $translation->getBCEntity() : $translation;
  }

511
  /**
512
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::setEntity().
513
   */
514 515 516
  public function setEntity(EntityInterface $entity) {
    $this->entity = $entity;
    return $this;
517 518 519 520 521
  }

  /**
   * Prepares the entity object before the form is built first.
   */
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
  protected function prepareEntity() {}

  /**
   * Invokes the specified prepare hook variant.
   *
   * @param string $hook
   *   The hook variant name.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   */
  protected function prepareInvokeAll($hook, array &$form_state) {
    $implementations = $this->moduleHandler->getImplementations($hook);
    foreach ($implementations as $module) {
      $function = $module . '_' . $hook;
      if (function_exists($function)) {
        // Ensure we pass an updated translation object and form display at
        // each invocation, since they depend on form state which is alterable.
539
        $args = array($this->getTranslatedEntity($form_state), $this->getFormDisplay($form_state), $this->operation, &$form_state);
540 541 542
        call_user_func_array($function, $args);
      }
    }
543 544
  }

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  /**
   * {@inheritdoc}
   */
  public function getFormDisplay(array $form_state) {
    return isset($form_state['form_display']) ? $form_state['form_display'] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state) {
    $form_state['form_display'] = $form_display;
    return $this;
  }

560
  /**
561
   * Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation().
562 563 564 565 566
   */
  public function getOperation() {
    return $this->operation;
  }
}