Commit c7013bda authored by webchick's avatar webchick

Issue #2313931 by Berdir: Merge ['controller'] with...

Issue #2313931 by Berdir: Merge ['controller'] with ['build_info']['callback_object'] and add a method for it.
parent cd240fd2
......@@ -89,7 +89,7 @@ public function getFormId() {
public function buildForm(array $form, FormStateInterface $form_state) {
// During the initial form build, add this form object to the form state and
// allow for initial preparation before form building and processing.
if (!isset($form_state['controller'])) {
if (!isset($form_state['entity_form_initialized'])) {
$this->init($form_state);
}
......@@ -115,9 +115,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
* Initialize the form state and the entity before the first form build.
*/
protected function init(FormStateInterface $form_state) {
// Add the form object to the form state so it can be easily accessed by
// module-provided form handlers there.
$form_state['controller'] = $this;
// Flag that this form has been initialized.
$form_state['entity_form_initialized'] = TRUE;
// Prepare the entity to be presented in the entity form.
$this->prepareEntity();
......@@ -158,7 +157,7 @@ public function form(array $form, FormStateInterface $form_state) {
public function processForm($element, FormStateInterface $form_state, $form) {
// If the form is cached, process callbacks may not have a valid reference
// to the entity object, hence we must restore it.
$this->entity = $form_state['controller']->getEntity();
$this->entity = $form_state->getFormObject()->getEntity();
return $element;
}
......
......@@ -326,7 +326,7 @@ class FormState implements FormStateInterface, \ArrayAccess {
* conflict with ones used by the Form API or other modules is to use the
* module name as the key name or a prefix for the key name. For example, the
* entity form classes use $this->entity in entity forms, or
* $form_state['controller']->getEntity() outside the controller, to store
* $form_state->getFormObject()->getEntity() outside the controller, to store
* information about the entity being edited, and this information stays
* available across successive clicks of the "Preview" button (if available)
* as well as when the "Save" button is finally clicked.
......@@ -807,6 +807,14 @@ public function prepareCallback($callback) {
return $callback;
}
/**
* {@inheritdoc}
*/
public function getFormObject() {
$build_info = $this->get('build_info');
return $build_info['callback_object'];
}
/**
* Wraps drupal_set_message().
*
......
......@@ -470,4 +470,12 @@ public function setRebuild($rebuild = TRUE);
*/
public function prepareCallback($callback);
/**
* Returns the form object that is responsible for building this form.
*
* @return \Drupal\Core\Form\FormInterface
* The form object.
*/
public function getFormObject();
}
......@@ -161,7 +161,7 @@ function book_node_links_alter(array &$node_links, NodeInterface $node, array &$
* @see book_pick_book_nojs_submit()
*/
function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$node = $form_state['controller']->getEntity();
$node = $form_state->getFormObject()->getEntity();
$account = \Drupal::currentUser();
$access = $account->hasPermission('administer book outlines');
if (!$access) {
......@@ -217,7 +217,7 @@ function book_node_builder($entity_type, NodeInterface $entity, &$form, FormStat
* @see book_form_node_form_alter()
*/
function book_pick_book_nojs_submit($form, FormStateInterface $form_state) {
$node = $form_state['controller']->getEntity();
$node = $form_state->getFormObject()->getEntity();
$node->book = $form_state->getValue('book');
$form_state['rebuild'] = TRUE;
}
......
......@@ -170,7 +170,7 @@ function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
'#open' => TRUE,
'#weight' => 5,
);
$account = $form_state['controller']->getEntity();
$account = $form_state->getFormObject()->getEntity();
if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
$account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
}
......@@ -187,7 +187,7 @@ function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
* Submit callback for the user profile form to save the contact page setting.
*/
function contact_user_profile_form_submit($form, FormStateInterface $form_state) {
$account = $form_state['controller']->getEntity();
$account = $form_state->getFormObject()->getEntity();
if ($account->id() && $form_state->hasValue('contact')) {
\Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
}
......
......@@ -334,22 +334,6 @@ function content_translation_controller($entity_type_id) {
return new $class($entity_type);
}
/**
* Returns the entity form for the given form.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state holding the entity form.
*
* @return \Drupal\Core\Entity\EntityFormInterface;
* An instance of the content translation form interface or FALSE if not an
* entity form.
*
* @todo Move to \Drupal\content_translation\ContentTranslationManager.
*/
function content_translation_form_controller(FormStateInterface $form_state) {
return isset($form_state['controller']) && $form_state['controller'] instanceof EntityFormInterface ? $form_state['controller'] : FALSE;
}
/**
* Checks whether a content translation is accessible.
*
......@@ -430,8 +414,11 @@ function content_translation_permission() {
* Implements hook_form_alter().
*/
function content_translation_form_alter(array &$form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$entity = $form_controller ? $form_controller->getEntity() : NULL;
$form_object = $form_state->getFormObject();
if (!($form_object instanceof EntityFormInterface)) {
return;
}
$entity = $form_object->getEntity();
if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1) {
$controller = content_translation_controller($entity->getEntityTypeId());
......@@ -440,7 +427,7 @@ function content_translation_form_alter(array &$form, FormStateInterface $form_s
// @todo Move the following lines to the code generating the property form
// elements once we have an official #multilingual FAPI key.
$translations = $entity->getTranslationLanguages();
$form_langcode = $form_controller->getFormLangcode($form_state);
$form_langcode = $form_object->getFormLangcode($form_state);
// Handle fields shared between translations when there is at least one
// translation available or a new one is being created.
......
......@@ -84,8 +84,8 @@ public function getSourceLangcode(FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
$form_controller = content_translation_form_controller($form_state);
$form_langcode = $form_controller->getFormLangcode($form_state);
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
$entity_langcode = $entity->getUntranslated()->language()->id;
$source_langcode = $this->getSourceLangcode($form_state);
......@@ -95,7 +95,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
// Make sure a new translation does not appear as existing yet.
unset($translations[$form_langcode]);
}
$is_translation = !$form_controller->isDefaultFormLangcode($form_state);
$is_translation = !$form_object->isDefaultFormLangcode($form_state);
$has_translations = count($translations) > 1;
// Adjust page title to specify the current language being edited, if we
......@@ -377,8 +377,8 @@ protected function addTranslatabilityClue(&$element) {
* @see \Drupal\content_translation\ContentTranslationHandler::entityFormAlter()
*/
public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$form_langcode = $form_controller->getFormLangcode($form_state);
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
if (!isset($entity->translation[$form_langcode])) {
$entity->translation[$form_langcode] = array();
......@@ -435,15 +435,15 @@ function entityFormValidate($form, FormStateInterface $form_state) {
* Takes care of the source language change.
*/
public function entityFormSourceChange($form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$entity = $form_controller->getEntity();
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$source = $form_state->getValue(array('source_langcode', 'source'));
$entity_type_id = $entity->getEntityTypeId();
$form_state->setRedirect('content_translation.translation_add_' . $entity_type_id, array(
$entity_type_id => $entity->id(),
'source' => $source,
'target' => $form_controller->getFormLangcode($form_state),
'target' => $form_object->getFormLangcode($form_state),
));
$languages = language_list();
drupal_set_message(t('Source language set to: %language', array('%language' => $languages[$source]->name)));
......@@ -455,8 +455,8 @@ public function entityFormSourceChange($form, FormStateInterface $form_state) {
* Takes care of entity deletion.
*/
function entityFormDelete($form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$entity = $form_controller->getEntity();
$form_object = $form_state->getFormObject()->getEntity();
$entity = $form_object->getEntity();
if (count($entity->getTranslationLanguages()) > 1) {
drupal_set_message(t('This will delete all the translations of %label.', array('%label' => $entity->label())), 'warning');
}
......@@ -468,12 +468,12 @@ function entityFormDelete($form, FormStateInterface $form_state) {
* Takes care of content translation deletion.
*/
function entityFormDeleteTranslation($form, FormStateInterface $form_state) {
$form_controller = content_translation_form_controller($form_state);
$entity = $form_controller->getEntity();
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$entity_type_id = $entity->getEntityTypeId();
$form_state->setRedirect('content_translation.translation_delete_' . $entity_type_id, array(
$entity_type_id => $entity->id(),
'language' => $form_controller->getFormLangcode($form_state),
'language' => $form_object->getFormLangcode($form_state),
));
}
......
......@@ -91,7 +91,7 @@ function editor_form_filter_admin_overview_alter(&$form, FormStateInterface $for
*/
function editor_form_filter_format_form_alter(&$form, FormStateInterface $form_state) {
if (!isset($form_state['editor'])) {
$format_id = $form_state['controller']->getEntity()->id();
$format_id = $form_state->getFormObject()->getEntity()->id();
$form_state['editor'] = editor_load($format_id);
}
$editor = $form_state['editor'];
......@@ -174,7 +174,7 @@ function editor_form_filter_admin_format_editor_configure($form, FormStateInterf
}
elseif (empty($editor) || $editor_value !== $editor->getEditor()) {
$editor = entity_create('editor', array(
'format' => $form_state['controller']->getEntity()->id(),
'format' => $form_state->getFormObject()->getEntity()->id(),
'editor' => $editor_value,
));
$form_state['editor'] = $editor;
......@@ -213,7 +213,7 @@ function editor_form_filter_admin_format_validate($form, FormStateInterface $for
*/
function editor_form_filter_admin_format_submit($form, FormStateInterface $form_state) {
// Delete the existing editor if disabling or switching between editors.
$format_id = $form_state['controller']->getEntity()->id();
$format_id = $form_state->getFormObject()->getEntity()->id();
$original_editor = editor_load($format_id);
if ($original_editor && $original_editor->getEditor() != $form_state->getValue('editor')) {
$original_editor->delete();
......
......@@ -151,7 +151,7 @@ function field_ui_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
*/
function field_ui_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
// We want to display the button only on add page.
if ($form_state['controller']->getEntity()->isNew()) {
if ($form_state->getFormObject()->getEntity()->isNew()) {
$form['actions']['save_continue'] = $form['actions']['submit'];
$form['actions']['save_continue']['#value'] = t('Save and manage fields');
$form['actions']['save_continue']['#weight'] = $form['actions']['save_continue']['#weight'] + 5;
......
......@@ -387,7 +387,7 @@ function forum_comment_delete(CommentInterface $comment) {
*/
function forum_form_taxonomy_vocabulary_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$vid = \Drupal::config('forum.settings')->get('vocabulary');
$vocabulary = $form_state['controller']->getEntity();
$vocabulary = $form_state->getFormObject()->getEntity();
if ($vid == $vocabulary->id()) {
$form['help_forum_vocab'] = array(
'#markup' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
......@@ -419,7 +419,7 @@ function forum_form_taxonomy_term_form_alter(&$form, FormStateInterface $form_st
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*/
function forum_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$node = $form_state['controller']->getEntity();
$node = $form_state->getFormObject()->getEntity();
if (isset($node->taxonomy_forums) && !$node->isNew()) {
$forum_terms = $node->taxonomy_forums;
// If editing, give option to leave shadows.
......
......@@ -321,7 +321,7 @@ function menu_ui_node_prepare_form(NodeInterface $node, $operation, FormStateInt
function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) {
// Generate a list of possible parents (not including this link or descendants).
// @todo This must be handled in a #process handler.
$node = $form_state['controller']->getEntity();
$node = $form_state->getFormObject()->getEntity();
$definition = $form_state['menu_link_definition'];
$type = $node->getType();
/** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
......@@ -446,7 +446,7 @@ function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_stat
/** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
$menu_parent_selector = \Drupal::service('menu.parent_form_selector');
$menu_options = menu_ui_get_menus();
$type = $form_state['controller']->getEntity();
$type = $form_state->getFormObject()->getEntity();
if ($type->id()) {
$config_values = \Drupal::config('menu.entity.node.' . $type->id())->get();
}
......@@ -516,7 +516,7 @@ function menu_ui_form_node_type_form_validate(&$form, FormStateInterface $form_s
* @see menu_ui_form_node_type_form_alter().
*/
function menu_ui_form_node_type_form_submit(&$form, FormStateInterface $form_state) {
$type = $form_state['controller']->getEntity();
$type = $form_state->getFormObject()->getEntity();
\Drupal::config('menu.entity.node.' . $type->id())
->set('available_menus', array_values(array_filter($form_state->getValue('menu_options'))))
->set('parent', $form_state->getValue('menu_parent'))
......
......@@ -95,7 +95,7 @@ public function form(array $form, FormStateInterface $form_state) {
// Rebuild the form.
$form_state['rebuild'] = TRUE;
$this->entity = $preview['controller']->getEntity();
$this->entity = $preview->getFormObject()->getEntity();
unset($this->entity->in_preview);
}
......
......@@ -40,8 +40,8 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
$form['content_translation']['created']['#access'] = FALSE;
}
$form_controller = content_translation_form_controller($form_state);
$form_langcode = $form_controller->getFormLangcode($form_state);
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
$translations = $entity->getTranslationLanguages();
$status_translatable = NULL;
// Change the submit button labels if there was a status field they affect
......@@ -76,9 +76,9 @@ protected function entityFormTitle(EntityInterface $entity) {
*/
public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
if ($form_state->hasValue('content_translation')) {
$form_controller = content_translation_form_controller($form_state);
$form_object = $form_state->getFormObject();
$translation = &$form_state->getValue('content_translation');
$translation['status'] = $form_controller->getEntity()->isPublished();
$translation['status'] = $form_object->getEntity()->isPublished();
// $form['content_translation']['name'] is the equivalent field
// for translation author uid.
$translation['name'] = $form_state->getValue('uid');
......
......@@ -41,7 +41,7 @@ public function __construct(TempStoreFactory $temp_store_factory) {
public function convert($value, $definition, $name, array $defaults, Request $request) {
$store = $this->tempStoreFactory->get('node_preview');
if ($form_state = $store->get($value)) {
return $form_state['controller']->getEntity();
return $form_state->getFormObject()->getEntity();
}
}
......
......@@ -56,7 +56,7 @@ function path_permission() {
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*/
function path_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state['controller']->getEntity();
$node = $form_state->getFormObject()->getEntity();
if ($node->hasField('path') && $node->get('path')->access('edit')) {
$form['path_settings'] = array(
'#type' => 'details',
......
......@@ -675,7 +675,7 @@ function hook_page_alter(&$page) {
*
* One popular use of this hook is to add form elements to the node form. When
* altering a node form, the node entity can be retrieved by invoking
* $form_state['controller']->getEntity().
* $form_state->getFormObject()->getEntity().
*
* In addition to hook_form_alter(), which is called for all forms, there are
* two more specific form hooks available. The first,
......
......@@ -1024,7 +1024,7 @@ function system_user_login($account) {
function system_user_timezone(&$form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
$account = $form_state['controller']->getEntity();
$account = $form_state->getFormObject()->getEntity();
$form['timezone'] = array(
'#type' => 'details',
'#title' => t('Locale settings'),
......
......@@ -247,7 +247,7 @@ function entity_test_permission() {
* Implements hook_form_BASE_FORM_ID_alter().
*/
function entity_test_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$langcode = $form_state['controller']->getFormLangcode($form_state);
$langcode = $form_state->getFormObject()->getFormLangcode($form_state);
\Drupal::state()->set('entity_test.form_langcode', $langcode);
}
......
......@@ -33,7 +33,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
*/
function entityFormSave(array $form, FormStateInterface $form_state) {
if ($this->getSourceLangcode($form_state)) {
$entity = content_translation_form_controller($form_state)->getEntity();
$entity = $form_state->getFormObject()->getEntity();
// We need a redirect here, otherwise we would get an access denied page,
// since the current URL would be preserved and we would try to add a
// translation for a language that already has a translation.
......
......@@ -33,7 +33,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
*/
function entityFormSave(array $form, FormStateInterface $form_state) {
if ($this->getSourceLangcode($form_state)) {
$entity = content_translation_form_controller($form_state)->getEntity();
$entity = $form_state->getFormObject()->getEntity();
// We need a redirect here, otherwise we would get an access denied page
// since the current URL would be preserved and we would try to add a
// translation for a language that already has a translation.
......
......@@ -268,7 +268,7 @@ function seven_preprocess_maintenance_page(&$variables) {
*/
function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->get('controller')->getEntity();
$node = $form_state->getFormObject()->getEntity();
$form['#theme'] = array('node_edit_form');
$form['#attached']['css'][] = drupal_get_path('module', 'node') . '/css/node.module.css';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment