EntityFormInterface.php 3.77 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\Core\Entity;
9

10
use Drupal\Core\Extension\ModuleHandlerInterface;
11
use Drupal\Core\Form\BaseFormIdInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\StringTranslation\TranslationInterface;
14

15
/**
16
 * Defines an interface for entity form classes.
17
 */
18
interface EntityFormInterface extends BaseFormIdInterface {
19

20 21 22 23 24 25
  /**
   * Sets the operation for this form.
   *
   * @param string $operation
   *   The name of the current operation.
   *
26
   * @return $this
27 28 29
   */
  public function setOperation($operation);

30
  /**
31
   * Returns the operation identifying the form.
32 33 34 35 36 37 38 39 40 41 42
   *
   * @return string
   *   The name of the operation.
   */
  public function getOperation();

  /**
   * Returns the form entity.
   *
   * The form entity which has been used for populating form element defaults.
   *
43 44
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
45
   *
46
   * @return \Drupal\Core\Entity\EntityInterface
47 48
   *   The current form entity.
   */
49
  public function getEntity();
50 51 52 53 54 55

  /**
   * Sets the form entity.
   *
   * Sets the form entity which will be used for populating form element
   * defaults. Usually, the form entity gets updated by
56
   * \Drupal\Core\Entity\EntityFormInterface::submit(), however this may
57 58 59
   * be used to completely exchange the form entity, e.g. when preparing the
   * rebuild of a multi-step form.
   *
60
   * @param \Drupal\Core\Entity\EntityInterface $entity
61
   *   The entity the current form should operate upon.
62 63
   *
   * @return $this
64
   */
65
  public function setEntity(EntityInterface $entity);
66 67 68 69 70 71 72 73

  /**
   * Builds an updated entity object based upon the submitted form values.
   *
   * For building the updated entity object the form's entity is cloned and
   * the submitted form values are copied to entity properties. The form's
   * entity remains unchanged.
   *
74
   * @see \Drupal\Core\Entity\EntityFormInterface::getEntity()
75 76 77
   *
   * @param array $form
   *   A nested array form elements comprising the form.
78 79
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
80
   *
81
   * @return \Drupal\Core\Entity\EntityInterface
82 83
   *   An updated copy of the form's entity object.
   */
84
  public function buildEntity(array $form, FormStateInterface $form_state);
85 86 87 88 89 90

  /**
   * Validates the submitted form values of the entity form.
   *
   * @param array $form
   *   A nested array form elements comprising the form.
91 92
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
93
   */
94
  public function validate(array $form, FormStateInterface $form_state);
95 96

  /**
97
   * Form submission handler for the 'save' action.
98
   *
99 100
   * Normally this method should be overridden to provide specific messages to
   * the user and redirect the form after the entity has been saved.
101 102
   *
   * @param array $form
103
   *   An associative array containing the structure of the form.
104 105
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
106 107 108
   *
   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
109
   */
110
  public function save(array $form, FormStateInterface $form_state);
111

112
  /**
113
   * Sets the string translation service for this form.
114
   *
115
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
116 117
   *   The translation manager.
   *
118
   * @return $this
119
   */
120
  public function setStringTranslation(TranslationInterface $string_translation);
121 122 123 124 125 126 127

  /**
   * Sets the module handler for this form.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   *
128
   * @return $this
129 130 131
   */
  public function setModuleHandler(ModuleHandlerInterface $module_handler);

132
}