executable)) { $this->executable = Views::executableFactory()->get($this); } return $this->executable; } /** * Overrides Drupal\Core\Config\Entity\ConfigEntityBase::createDuplicate(). */ public function createDuplicate() { $duplicate = parent::createDuplicate(); unset($duplicate->executable); return $duplicate; } /** * Overrides \Drupal\Core\Entity\Entity::label(). * * When a certain view doesn't have a label return the ID. */ public function label() { if (!$label = $this->get('label')) { $label = $this->id(); } return $label; } /** * Adds a new display handler to the view, automatically creating an ID. * * @param string $plugin_id * (optional) The plugin type from the Views plugin annotation. Defaults to * 'page'. * @param string $title * (optional) The title of the display. Defaults to NULL. * @param string $id * (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults * to NULL. * * @return string|false * The key to the display in $view->display, or FALSE if no plugin ID was * provided. */ public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) { if (empty($plugin_id)) { return FALSE; } $plugin = Views::pluginManager('display')->getDefinition($plugin_id); if (empty($plugin)) { $plugin['title'] = t('Broken'); } if (empty($id)) { $id = $this->generateDisplayId($plugin_id); // Generate a unique human-readable name by inspecting the counter at the // end of the previous display ID, e.g., 'page_1'. if ($id !== 'default') { preg_match("/[0-9]+/", $id, $count); $count = $count[0]; } else { $count = ''; } if (empty($title)) { // If there is no title provided, use the plugin title, and if there are // multiple displays, append the count. $title = $plugin['title']; if ($count > 1) { $title .= ' ' . $count; } } } $display_options = array( 'display_plugin' => $plugin_id, 'id' => $id, 'display_title' => $title, 'position' => count($this->display), 'display_options' => array(), ); // Add the display options to the view. $this->display[$id] = $display_options; return $id; } /** * Generates a display ID of a certain plugin type. * * @param string $plugin_id * Which plugin should be used for the new display ID. */ protected function generateDisplayId($plugin_id) { // 'default' is singular and is unique, so just go with 'default' // for it. For all others, start counting. if ($plugin_id == 'default') { return 'default'; } // Initial ID. $id = $plugin_id . '_1'; $count = 1; // Loop through IDs based upon our style plugin name until // we find one that is unused. while (!empty($this->display[$id])) { $id = $plugin_id . '_' . ++$count; } return $id; } /** * {@inheritdoc} */ public function &getDisplay($display_id) { return $this->display[$display_id]; } /** * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties(); */ public function getExportProperties() { $names = array( 'base_field', 'base_table', 'core', 'description', 'status', 'display', 'label', 'module', 'id', 'tag', 'uuid', 'langcode', ); $properties = array(); foreach ($names as $name) { $properties[$name] = $this->get($name); } return $properties; } /** * {@inheritdoc} */ public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) { parent::postSave($storage_controller, $update); // Clear cache tags for this view. // @todo Remove if views implements a view_builder controller. $id = $this->id(); Cache::deleteTags(array('view' => array($id => $id))); views_invalidate_cache(); } /** * {@inheritdoc} */ public static function postLoad(EntityStorageControllerInterface $storage_controller, array &$entities) { parent::postLoad($storage_controller, $entities); foreach ($entities as $entity) { $entity->mergeDefaultDisplaysOptions(); } } /** * {@inheritdoc} */ public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) { parent::preCreate($storage_controller, $values); // If there is no information about displays available add at least the // default display. $values += array( 'display' => array( 'default' => array( 'display_plugin' => 'default', 'id' => 'default', 'display_title' => 'Master', 'position' => 0, 'display_options' => array(), ), ) ); } /** * {@inheritdoc} */ public function postCreate(EntityStorageControllerInterface $storage_controller) { parent::postCreate($storage_controller); $this->mergeDefaultDisplaysOptions(); } /** * {@inheritdoc} */ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) { parent::postDelete($storage_controller, $entities); $tempstore = \Drupal::service('user.tempstore')->get('views'); $tags = array(); foreach ($entities as $entity) { $id = $entity->id(); $tempstore->delete($id); $tags['view'][$id] = $id; } // Clear cache tags for these views. // @todo Remove if views implements a view_builder controller. Cache::deleteTags($tags); } /** * {@inheritdoc} */ public function mergeDefaultDisplaysOptions() { $displays = array(); foreach ($this->get('display') as $key => $options) { $options += array( 'display_options' => array(), 'display_plugin' => NULL, 'id' => NULL, 'display_title' => '', 'position' => NULL, ); // Add the defaults for the display. $displays[$key] = $options; } // Sort the displays. uasort($displays, function ($display1, $display2) { if ($display1['position'] != $display2['position']) { return $display1['position'] < $display2['position'] ? -1 : 1; } return 0; }); $this->set('display', $displays); } }