diff --git a/lib/Drupal/views/Plugin/views_ui_listing/listing/ViewListing.php b/lib/Drupal/views/Plugin/views_ui_listing/listing/ViewListing.php index 26e9fec..4ec9138 100644 --- a/lib/Drupal/views/Plugin/views_ui_listing/listing/ViewListing.php +++ b/lib/Drupal/views/Plugin/views_ui_listing/listing/ViewListing.php @@ -29,11 +29,34 @@ class ViewListing extends ConfigEntityListingBase { * Overrides Drupal\config\Plugin\ConfigEntityListingBase::hookMenu(); */ public function hookMenu() { + // Find the path and the number of path arguments. + $path = $this->definition['path']; + $path_count = count(explode('/', $path)); + $items = parent::hookMenu(); // Override the access callback. // @todo Probably won't need to specify user access. - $items[$this->definition['path']]['access callback'] = 'user_access'; - $items[$this->definition['path']]['access arguments'] = array('administer views'); + $items[$path]['access callback'] = 'user_access'; + $items[$path]['access arguments'] = array('administer views'); + + // Set up the base for AJAX callbacks. + $ajax_base = array( + 'page callback' => 'views_ui_listing_ajax_callback', + 'page arguments' => array($this, $path_count + 1, $path_count + 2), + 'access callback' => 'user_access', + 'access arguments' => array('administer views'), + 'type' => MENU_CALLBACK, + ); + + // Add an enable link. + $items["$path/view/%views_ui_view/enable"] = array( + 'title' => 'Enable a view', + ) + $ajax_base; + // Add a disable link. + $items["$path/view/%views_ui_view/disable"] = array( + 'title' => 'Disable a view', + ) + $ajax_base; + return $items; } diff --git a/lib/Drupal/views/ViewStorage.php b/lib/Drupal/views/ViewStorage.php index 66e2626..f1f9acc 100644 --- a/lib/Drupal/views/ViewStorage.php +++ b/lib/Drupal/views/ViewStorage.php @@ -26,7 +26,7 @@ public function id() { */ public function enable() { $this->disabled = FALSE; - // @todo Save the new view state here? + $this->save(); } /** @@ -34,7 +34,7 @@ public function enable() { */ public function disable() { $this->disabled = TRUE; - // @todo Save the new view state here? + $this->save(); } /** diff --git a/views_ui.module b/views_ui.module index 7349f70..dc8246c 100644 --- a/views_ui.module +++ b/views_ui.module @@ -27,19 +27,6 @@ function views_ui_menu() { 'type' => MENU_LOCAL_ACTION, ) + $base; - // Export UI replacements* - $items['admin/structure/views/view/%views_ui_view/enable'] = array( - 'title' => 'Enable a view', - 'page callback' => 'views_ui_view_enable', - 'page arguments' => array(4), - ) + $base; - - $items['admin/structure/views/view/%views_ui_view/disable'] = array( - 'title' => 'Enable a view', - 'page callback' => 'views_ui_view_disable', - 'page arguments' => array(4), - ) + $base; - /* // Top-level Views module pages (not tied to a particular View). $items['admin/structure/views/add-template'] = array( @@ -766,19 +753,3 @@ function views_ui_truncate($string, $length) { function views_ui_view_load($name) { return views_get_view($name); } - -/** - * Page callback to enable a view. - */ -function views_ui_view_enable($view) { - $view->enable(); - $view->save(); -} - -/** - * Page callback to disable a view. - */ -function views_ui_view_disable($view) { - $view->disable(); - $view->save(); -} diff --git a/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingBase.php b/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingBase.php index fe9afcc..79d79df 100644 --- a/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingBase.php +++ b/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingBase.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\config\ConfigStorageController; use Drupal\entity\EntityInterface; +use Symfony\Component\HttpFoundation\JsonResponse; /** * Abstract base class for config entity listing plugins. @@ -57,6 +58,10 @@ public function getController() { return $this->controller; } + public function getPath() { + return $this->definition['path']; + } + /** * Implements Drupal\config\Plugin\ConfigEntityListingInterface::hookMenu(); */ @@ -149,7 +154,21 @@ public function renderList() { '#theme' => 'table', '#header' => $this->getHeaderData(), '#rows' => $rows, + '#attributes' => array( + 'id' => 'config-entity-listing', + ), ); } + /** + * Implements Drupal\config\Plugin\ConfigEntityListingInterface::renderList(); + */ + public function renderListAJAX() { + $list = $this->renderList(); + $commands = array(); + $commands[] = ajax_command_replace('#config-entity-listing', drupal_render($list)); + + return new JsonResponse(ajax_render($commands)); + } + } diff --git a/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingInterface.php b/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingInterface.php index e6b3604..b631bb0 100644 --- a/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingInterface.php +++ b/views_ui_listing/lib/Drupal/views_ui_listing/Plugin/ConfigEntityListingInterface.php @@ -60,6 +60,14 @@ public function getHeaderData(); public function renderList(); /** + * Returns the list page as JSON. + * + * @return Symfony\Component\HttpFoundation\JsonResponse + * AJAX commands to render the list. + */ + public function renderListAJAX(); + + /** * Renders a list of action links. * * @return array diff --git a/views_ui_listing/views_ui_listing.module b/views_ui_listing/views_ui_listing.module index f31b9d0..7f55f5b 100644 --- a/views_ui_listing/views_ui_listing.module +++ b/views_ui_listing/views_ui_listing.module @@ -1,6 +1,8 @@ createInstance($plugin_id); + $listing = views_ui_listing_get_plugin($plugin_id); return $listing->renderList(); } + +/** + * Gets an instance of a listing plugin. + * + * @param string $plugin_id + * The config entity plugin ID. + * + * @return Drupal\config\Plugin\ConfigEntityListingInterface + * @ + */ +function views_ui_listing_get_plugin($plugin_id) { + // @todo Add some error handling here, or in the manager class. + $listing_manager = new ConfigEntityListingManager(); + return $listing_manager->createInstance($plugin_id); +} + +/** + * Page callback: Calls a method on a config entity and reloads the listing page. + * + * @param Drupal\views_ui_listing\Plugin\ConfigEntityListingInterface $listing + * The listing plugin instance for this entity. + * @param Drupal\config\ConfigEntityInterface $entity + * The config entity being acted upon. + * @param string $op + * The action to perform, e.g., 'enable' or 'disable'. + * + * @return mixed + * Either returns the listing page as JSON, or calls drupal_goto() to + * redirect back to the listing page. + */ +function views_ui_listing_ajax_callback(ConfigEntityListingInterface $listing, ConfigEntityInterface $entity, $op) { + // Perform the operation. + $entity->$op(); + + // If the request is via AJAX, return the rendered list as JSON. + if (drupal_container()->get('request')->request->get('js')) { + return $listing->renderListAJAX(); + } + // Otherwise, redirect back to the page. + else { + drupal_goto($listing->getPath()); + } +}