Commit 34acf68f authored by alexpott's avatar alexpott

Issue #1919700 by damiankloip, tim.plunkett: Replace views_ui_cache_set() with...

Issue #1919700 by damiankloip, tim.plunkett: Replace views_ui_cache_set() with a method on the ViewUI object.
parent bb2cdb26
......@@ -836,7 +836,7 @@ public function displayExposedForm($form, &$form_state) {
$form_state['view']->addFormToStack($form_state['form_key'], $form_state['display_id'], $form_state['type'], $form_state['id'], TRUE, TRUE);
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
$form_state['force_expose_options'] = TRUE;
......@@ -898,7 +898,7 @@ public function submitTemporaryForm($form, &$form_state) {
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
// Write to cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -2039,7 +2039,7 @@ public function changeThemeForm($form, &$form_state) {
// This is just a temporary variable.
$form_state['view']->theme = $form_state['values']['theme'];
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
}
......
......@@ -430,7 +430,7 @@ public function buildGroupForm($form, &$form_state) {
$form_state['view']->addFormToStack($form_state['form_key'], $form_state['display_id'], $form_state['type'], $form_state['id'], TRUE, TRUE);
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
$form_state['force_build_group_options'] = TRUE;
......@@ -1095,7 +1095,7 @@ public function addGroupForm($form, &$form_state) {
$form_state['view']->get('executable')->setItem($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
$form_state['rerender'] = TRUE;
$form_state['rebuild'] = TRUE;
$form_state['force_build_group_options'] = TRUE;
......
......@@ -31,26 +31,32 @@ public static function getInfo() {
* Tests the user tempstore views data in the UI.
*/
public function testCacheData() {
$view = entity_load('view', 'test_view');
$views_admin_user_uid = $this->fullAdminUser->id();
$temp_store = $this->container->get('user.tempstore')->get('views');
$view_cache = $temp_store->getMetadata('test_view');
// The view should not be locked.
$this->assertFalse($view_cache, 'The view is not locked.');
$this->assertEqual($temp_store->getMetadata('test_view'), NULL, 'The view is not locked.');
$this->drupalGet('admin/structure/views/view/test_view/edit');
// Make sure we have 'changes' to the view.
$this->drupalPost('admin/structure/views/nojs/display/test_view/default/title', array(), t('Apply'));
$this->assertText('You have unsaved changes.');
$this->assertEqual($temp_store->getMetadata('test_view')->owner, $views_admin_user_uid, 'View cache has been saved.');
$view_cache = $temp_store->get('test_view');
// The view should be enabled.
$this->assertTrue($view_cache->status(), 'The view is enabled.');
// The view should now be locked.
$view_cache = $temp_store->getMetadata('test_view');
$this->assertTrue($view_cache, 'The view is locked.');
$this->assertEqual($temp_store->getMetadata('test_view')->owner, $views_admin_user_uid, 'The view is locked.');
// Cancel the view edit and make sure the cache is deleted.
$this->drupalPost(NULL, array(), t('Cancel'));
$this->assertEqual($temp_store->getMetadata('test_view'), NULL, 'User tempstore data has been removed.');
// Test we are redirected to the view listing page.
$this->assertUrl('admin/structure/views', array(), 'Redirected back to the view listing page.');
// Login with another user and make sure the view is locked and break.
$this->drupalPost('admin/structure/views/nojs/display/test_view/default/title', array(), t('Apply'));
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/views/view/test_view/edit');
......
......@@ -14,6 +14,20 @@
*/
abstract class UITestBase extends ViewTestBase {
/**
* An admin user with the 'administer views' permission.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $adminUser;
/**
* An admin user with administrative permissions for views, blocks, and nodes.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $fullAdminUser;
/**
* Modules to enable.
*
......@@ -28,8 +42,8 @@ protected function setUp() {
$this->adminUser = $this->drupalCreateUser(array('administer views'));
$views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view all revisions'));
$this->drupalLogin($views_admin);
$this->fullAdminUser = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view all revisions'));
$this->drupalLogin($this->fullAdminUser);
}
}
......@@ -178,7 +178,7 @@ public function buildForm(array $form, array &$form_state) {
}
if ($save_ui_cache) {
views_ui_cache_set($view);
$view->cacheSet();
}
return $form;
......@@ -245,7 +245,7 @@ public function submitForm(array &$form, array &$form_state) {
}
// Write to cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
/**
......@@ -263,7 +263,7 @@ public function remove(&$form, &$form_state) {
$executable->removeItem($form_state['display_id'], $form_state['type'], $form_state['id']);
// Write to cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -115,7 +115,7 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['view']->get('executable')->setItem($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
// Write to cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -109,7 +109,7 @@ public function submitForm(array &$form, array &$form_state) {
$executable->setItem($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
// Write to cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -113,7 +113,7 @@ public function validateForm(array &$form, array &$form_state) {
public function submitForm(array &$form, array &$form_state) {
$form_state['view']->get('executable')->displayHandlers->get($form_state['display_id'])->submitOptionsForm($form['options'], $form_state);
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -90,7 +90,8 @@ public function submitForm(array &$form, array &$form_state) {
if (isset($bases[$view->get('base_table')])) {
$form_state['#page_title'] .= ' (' . $bases[$view->get('base_table')]['title'] . ')';
}
views_ui_cache_set($view);
$view->cacheSet();
}
}
......@@ -160,7 +160,7 @@ public function submitForm(array &$form, array &$form_state) {
$display->setOption($types[$form_state['type']]['plural'], $new_fields);
// Store in cache
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......@@ -311,7 +311,7 @@ public function submitForm(array &$form, array &$form_state) {
}
// Store in cache.
views_ui_cache_set($form_state['view']);
$form_state['view']->cacheSet();
}
}
......
......@@ -133,7 +133,7 @@ public function submitForm(array &$form, array &$form_state) {
$view->set('display', $displays);
// Store in cache
views_ui_cache_set($view);
$view->cacheSet();
$form_state['redirect'] = array('admin/structure/views/view/' . $view->id() . '/edit', array('fragment' => 'views-tab-default'));
}
......
......@@ -90,7 +90,7 @@ public function process(array &$variables, Route $route, array &$converted) {
else {
$view->disable();
}
$view->locked = $temp_store->getMetadata($variables[$name]->id());
$view->lock = $temp_store->getMetadata($variables[$name]->id());
}
// Otherwise, decorate the existing view for use in the UI.
else {
......
......@@ -73,11 +73,11 @@ public function form(array $form, array &$form_state) {
$form['#attributes']['class'] = array('form-edit');
if (isset($view->locked) && is_object($view->locked) && $view->locked->owner != $GLOBALS['user']->uid) {
if ($view->isLocked()) {
$form['locked'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('view-locked', 'messages', 'warning')),
'#children' => t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => theme('username', array('account' => user_load($view->locked->owner))), '!age' => format_interval(REQUEST_TIME - $view->locked->updated), '!break' => url('admin/structure/views/view/' . $view->id() . '/break-lock'))),
'#children' => t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => theme('username', array('account' => user_load($view->lock->owner))), '!age' => format_interval(REQUEST_TIME - $view->lock->updated), '!break' => url('admin/structure/views/view/' . $view->id() . '/break-lock'))),
'#weight' => -10,
);
}
......@@ -529,7 +529,7 @@ public function submitDisplayUndoDelete($form, &$form_state) {
$view->set('display', $displays);
// Store in cache
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the top-level edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $id;
......@@ -545,7 +545,7 @@ public function submitDisplayEnable($form, &$form_state) {
$view->get('executable')->displayHandlers->get($id)->setOption('enabled', TRUE);
// Store in cache
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the top-level edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $id;
......@@ -560,7 +560,7 @@ public function submitDisplayDisable($form, &$form_state) {
$view->get('executable')->displayHandlers->get($id)->setOption('enabled', FALSE);
// Store in cache
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the top-level edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $id;
......@@ -577,7 +577,7 @@ public function submitDisplayDelete($form, &$form_state) {
$displays = $view->get('display');
$displays[$display_id]['deleted'] = TRUE;
$view->set('display', $displays);
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the top-level edit page. The first remaining display will
// become the active display.
......@@ -743,7 +743,7 @@ public function submitDisplayDuplicate($form, &$form_state) {
// By setting the current display the changed marker will appear on the new
// display.
$view->get('executable')->current_display = $new_display_id;
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the new display's edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $new_display_id;
......@@ -761,7 +761,7 @@ public function submitDisplayAdd($form, &$form_state) {
// A new display got added so the asterisks symbol should appear on the new
// display.
$view->get('executable')->current_display = $display_id;
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the new display's edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $display_id;
......@@ -793,7 +793,7 @@ public function submitCloneDisplayAsType($form, &$form_state) {
// By setting the current display the changed marker will appear on the new
// display.
$view->get('executable')->current_display = $new_display_id;
views_ui_cache_set($view);
$view->cacheSet();
// Redirect to the new display's edit page.
$form_state['redirect'] = 'admin/structure/views/view/' . $view->id() . '/edit/' . $new_display_id;
......
......@@ -59,9 +59,12 @@ class ViewUI implements ViewStorageInterface {
/**
* If this view is locked for editing.
*
* @var bool
* If this view is locked it will contain the result of
* \Drupal\user\TempStore::getMetadata(). Which can be a stdClass or NULL.
*
* @var stdClass
*/
public $locked;
public $lock;
/**
* If this view has been changed.
......@@ -201,7 +204,7 @@ public function standardSubmit($form, &$form_state) {
$display->optionsOverride($form, $form_state);
// Don't execute the normal submit handling but still store the changed view into cache.
views_ui_cache_set($this);
$this->cacheSet();
return;
}
elseif ($was_defaulted === $is_defaulted) {
......@@ -245,7 +248,7 @@ public function standardSubmit($form, &$form_state) {
public function standardCancel($form, &$form_state) {
if (!empty($this->changed) && isset($this->form_cache)) {
unset($this->form_cache);
views_ui_cache_set($this);
$this->cacheSet();
}
$form_state['redirect'] = 'admin/structure/views/view/' . $this->id() . '/edit';
......@@ -481,7 +484,7 @@ public function submitItemAdd($form, &$form_state) {
}
// Store in cache
views_ui_cache_set($this);
$this->cacheSet();
}
/**
......@@ -735,6 +738,43 @@ public function getFormProgress() {
return $progress;
}
/**
* Sets a cached view object in the user tempstore.
*/
public function cacheSet() {
if ($this->isLocked()) {
drupal_set_message(t('Changes cannot be made to a locked view.'), 'error');
return;
}
// Let any future object know that this view has changed.
$this->changed = TRUE;
$executable = $this->get('executable');
if (isset($executable->current_display)) {
// Add the knowledge of the changed display, too.
$this->changed_display[$executable->current_display] = TRUE;
unset($executable->current_display);
}
// Unset handlers; we don't want to write these into the cache.
unset($executable->display_handler);
unset($executable->default_display);
$executable->query = NULL;
unset($executable->displayHandlers);
\Drupal::service('user.tempstore')->get('views')->set($this->id(), $this);
}
/**
* Returns whether the current view is locked.
*
* @return bool
* TRUE if the view is locked, FALSE otherwise.
*/
public function isLocked() {
return is_object($this->lock) && ($this->lock->owner != $GLOBALS['user']->uid);
}
/**
* Passes through all unknown calls onto the storage object.
*/
......
......@@ -204,33 +204,6 @@ function views_ui_library_info() {
return $libraries;
}
/**
* Specialized cache function to add a flag to our view, include an appropriate
* include, and cache more easily.
*/
function views_ui_cache_set(ViewUI $view) {
if (isset($view->locked) && is_object($view->locked) && $view->locked->owner != $GLOBALS['user']->uid) {
drupal_set_message(t('Changes cannot be made to a locked view.'), 'error');
return;
}
$view->changed = TRUE; // let any future object know that this view has changed.
$executable = $view->get('executable');
if (isset($executable->current_display)) {
// Add the knowledge of the changed display, too.
$view->changed_display[$executable->current_display] = TRUE;
unset($executable->current_display);
}
// Unset handlers; we don't want to write these into the cache
unset($executable->display_handler);
unset($executable->default_display);
$executable->query = NULL;
unset($executable->displayHandlers);
drupal_container()->get('user.tempstore')->get('views')->set($view->id(), $view);
}
/**
* Theme preprocess for views-view.tpl.php.
*/
......
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