Commit a395309c authored by catch's avatar catch

Issue #1913742 by tim.plunkett: Allow custom form state to be passed to entity_get_form().

parent 386bfa9f
......@@ -448,13 +448,12 @@ function entity_form_id(EntityInterface $entity, $operation = 'default') {
* @return
* A $form_state array already filled the entity form controller.
*/
function entity_form_state_defaults(EntityInterface $entity, $operation = 'default', $langcode = NULL) {
function entity_form_state_defaults(EntityInterface $entity, $operation = 'default') {
$form_state = array();
$controller = drupal_container()->get('plugin.manager.entity')->getFormController($entity->entityType(), $operation);
$form_state['build_info']['callback'] = array($controller, 'build');
$form_state['build_info']['base_form_id'] = $entity->entityType() . '_form';
$form_state['build_info']['args'] = array($entity);
$form_state['langcode'] = $langcode;
return $form_state;
}
......@@ -484,12 +483,19 @@ function entity_form_submit(EntityInterface $entity, $operation = 'default', &$f
* The entity to be created or edited.
* @param $operation
* (optional) The operation identifying the form variation to be returned.
* @param array $form_state
* (optional) An associative array containing the current state of the form.
* Use this to pass additional information to the form, such as the langcode.
* @code
* $form_state['langcode'] = $langcode;
* $form = entity_get_form($entity, 'default', $form_state);
* @endcode
*
* @return
* The processed form for the given entity and operation.
*/
function entity_get_form(EntityInterface $entity, $operation = 'default', $langcode = NULL) {
$form_state = entity_form_state_defaults($entity, $operation, $langcode);
function entity_get_form(EntityInterface $entity, $operation = 'default', array $form_state = array()) {
$form_state += entity_form_state_defaults($entity, $operation);
$form_id = entity_form_id($entity, $operation);
return drupal_build_form($form_id, $form_state);
}
......
......@@ -80,7 +80,8 @@ function testEntityFormLanguage() {
// Explicitly set form langcode.
$langcode = $this->langcodes[0];
entity_get_form($node, 'default', $langcode);
$form_state['langcode'] = $langcode;
entity_get_form($node, 'default', $form_state);
$form_langcode = variable_get('entity_form_langcode', FALSE);
$this->assertTrue($langcode == $form_langcode, 'Form language is the same as the language parameter.');
......
......@@ -196,13 +196,12 @@ function translation_entity_add_page(EntityInterface $entity, Language $source =
translation_entity_prepare_translation($entity, $source, $target);
$info = $entity->entityInfo();
$operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
$form_state = entity_form_state_defaults($entity, $operation, $target->langcode);
$form_state['langcode'] = $target->langcode;
$form_state['translation_entity']['source'] = $source;
$form_state['translation_entity']['target'] = $target;
$controller = translation_entity_controller($entity->entityType());
$form_state['translation_entity']['translation_form'] = !$controller->getAccess($entity, 'update');
$form_id = entity_form_id($entity);
return drupal_build_form($form_id, $form_state);
return entity_get_form($entity, $operation, $form_state);
}
/**
......@@ -221,10 +220,9 @@ function translation_entity_edit_page(EntityInterface $entity, Language $languag
$language = !empty($language) ? $language : language(LANGUAGE_TYPE_CONTENT);
$info = $entity->entityInfo();
$operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
$form_state = entity_form_state_defaults($entity, $operation, $language->langcode);
$form_state['langcode'] = $language->langcode;
$form_state['translation_entity']['translation_form'] = TRUE;
$form_id = entity_form_id($entity);
return drupal_build_form($form_id, $form_state);
return entity_get_form($entity, $operation, $form_state);
}
/**
......
......@@ -293,16 +293,6 @@ function views_ui_break_lock_confirm($form, &$form_state, ViewUI $view) {
return $form;
}
/**
* Page callback for the Edit View page.
*/
function views_ui_edit_page(ViewUI $view, $display_id = NULL) {
$view->displayID = $display_id;
$build['edit'] = entity_get_form($view, 'edit');
$build['preview'] = entity_get_form($view, 'preview');
return $build;
}
/**
* Move form elements into details for presentation purposes.
*
......
......@@ -295,9 +295,8 @@ public function edit(ViewStorageInterface $view, $display_id = NULL) {
}
drupal_set_title($name);
$view_ui->displayID = $display_id;
$build['edit'] = entity_get_form($view_ui, 'edit');
$build['preview'] = entity_get_form($view_ui, 'preview');
$build['edit'] = entity_get_form($view_ui, 'edit', array('display_id' => $display_id));
$build['preview'] = entity_get_form($view_ui, 'preview', array('display_id' => $display_id));
return $build;
}
......@@ -316,8 +315,7 @@ public function edit(ViewStorageInterface $view, $display_id = NULL) {
public function preview(ViewStorageInterface $view, $display_id = NULL) {
$view_ui = $this->getViewUI($view);
$view_ui->displayID = $display_id;
return entity_get_form($view_ui, 'preview');
return entity_get_form($view_ui, 'preview', array('display_id' => $display_id));
}
/**
......
......@@ -37,7 +37,7 @@ public function setEntity(EntityInterface $entity, array &$form_state) {
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
public function form(array $form, array &$form_state, EntityInterface $view) {
$display_id = $view->displayID;
$display_id = $this->displayID;
// Do not allow the form to be cached, because $form_state['view'] can become
// stale between page requests.
// See views_ui_ajax_get_form() for how this affects #ajax.
......@@ -318,7 +318,7 @@ protected function actionsElement(array $form, array &$form_state) {
*/
public function getDisplayTab($view) {
$build = array();
$display_id = $view->displayID;
$display_id = $this->displayID;
$display = $view->get('executable')->displayHandlers->get($display_id);
// If the plugin doesn't exist, display an error message instead of an edit
// page.
......@@ -619,7 +619,7 @@ public function submitDisplayDelete($form, &$form_state) {
* The display ID of the tab to regenerate.
*/
public function rebuildCurrentTab(ViewUI $view, AjaxResponse $response, $display_id) {
$view->displayID = $display_id;
$this->displayID = $display_id;
if (!$view->get('executable')->setDisplay('default')) {
return;
}
......@@ -639,7 +639,7 @@ public function rebuildCurrentTab(ViewUI $view, AjaxResponse $response, $display
* Render the top of the display so it can be updated during ajax operations.
*/
public function renderDisplayTop(ViewUI $view) {
$display_id = $view->displayID;
$display_id = $this->displayID;
$element['#theme_wrappers'][] = 'views_ui_container';
$element['#attributes']['class'] = array('views-display-top', 'clearfix');
$element['#attributes']['id'] = array('views-display-top');
......@@ -755,7 +755,7 @@ public function submitDelayDestination($form, &$form_state) {
*/
public function submitDisplayDuplicate($form, &$form_state) {
$view = $this->getEntity($form_state);
$display_id = $view->displayID;
$display_id = $this->displayID;
// Create the new display.
$displays = $view->get('display');
......@@ -796,7 +796,7 @@ public function submitDisplayAdd($form, &$form_state) {
*/
public function submitCloneDisplayAsType($form, &$form_state) {
$view = $this->getEntity($form_state);
$display_id = $view->displayID;
$display_id = $this->displayID;
// Create the new display.
$parents = $form_state['triggering_element']['#parents'];
......
......@@ -18,13 +18,24 @@
abstract class ViewFormControllerBase extends EntityFormController {
/**
* Overrides \Drupal\Core\Entity\EntityFormController::init().
* The name of the display used by the form.
*
* @var string
*/
protected $displayID;
/**
* Overrides \Drupal\Core\Entity\EntityFormController::build().
*/
protected function init(array &$form_state, EntityInterface $entity) {
parent::init($form_state, $entity);
public function build(array $form, array &$form_state, EntityInterface $entity) {
if (isset($form_state['display_id'])) {
$this->displayID = $form_state['display_id'];
}
// @todo Remove the need for this.
form_load_include($form_state, 'inc', 'views_ui', 'admin');
return parent::build($form, $form_state, $entity);
}
/**
......@@ -34,25 +45,25 @@ protected function prepareEntity(EntityInterface $view) {
// Determine the displays available for editing.
if ($tabs = $this->getDisplayTabs($view)) {
// If a display isn't specified, use the first one.
if (empty($view->displayID)) {
if (empty($this->displayID)) {
foreach ($tabs as $id => $tab) {
if (!isset($tab['#access']) || $tab['#access']) {
$view->displayID = $id;
$this->displayID = $id;
break;
}
}
}
// If a display is specified, but we don't have access to it, return
// an access denied page.
if ($view->displayID && !isset($tabs[$view->displayID])) {
if ($this->displayID && !isset($tabs[$this->displayID])) {
throw new NotFoundHttpException();
}
elseif ($view->displayID && (isset($tabs[$view->displayID]['#access']) && !$tabs[$view->displayID]['#access'])) {
elseif ($this->displayID && (isset($tabs[$this->displayID]['#access']) && !$tabs[$this->displayID]['#access'])) {
throw new AccessDeniedHttpException();
}
}
elseif ($view->displayID) {
elseif ($this->displayID) {
throw new NotFoundHttpException();
}
}
......@@ -90,7 +101,7 @@ public static function getAdminCSS() {
* The display_id which is edited on the current request.
*/
public function getDisplayTabs(ViewUI $view) {
$display_id = $view->displayID;
$display_id = $this->displayID;
$tabs = array();
// Create a tab for each display.
......
......@@ -58,10 +58,10 @@ public function form(array $form, array &$form_state, EntityInterface $view) {
'#weight' => 110,
'#theme_wrappers' => array('container'),
'#attributes' => array('id' => 'views-live-preview'),
'#markup' => $view->renderPreview($view->displayID, $args),
'#markup' => $view->renderPreview($this->displayID, $args),
);
}
$form['#action'] = url('admin/structure/views/view/' . $view->id() .'/preview/' . $view->displayID);
$form['#action'] = url('admin/structure/views/view/' . $view->id() .'/preview/' . $this->displayID);
return $form;
}
......@@ -82,7 +82,7 @@ protected function actions(array $form, array &$form_state) {
'#submit' => array(array($this, 'submitPreview')),
'#id' => 'preview-submit',
'#ajax' => array(
'path' => 'admin/structure/views/view/' . $view->id() . '/preview/' . $view->displayID,
'path' => 'admin/structure/views/view/' . $view->id() . '/preview/' . $this->displayID,
'wrapper' => 'views-preview-wrapper',
'event' => 'click',
'progress' => array('type' => 'throbber'),
......
......@@ -96,8 +96,6 @@ class ViewUI implements ViewStorageInterface {
*/
public $live_preview;
public $displayID;
public $renderPreview = FALSE;
/**
......
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