3 namespace Drupal\views;
6 * Static service container wrapper for views.
11 * The translation manager.
13 * @var \Drupal\Core\StringTranslation\TranslationInterface
15 protected static $translationManager;
18 * A static cache for handler types data.
22 protected static $handlerTypes;
25 * A list of all available views plugin types.
29 protected static $plugins = [
32 'argument' => 'handler',
33 'argument_default' => 'plugin',
34 'argument_validator' => 'plugin',
36 'display_extender' => 'plugin',
37 'display' => 'plugin',
38 'exposed_form' => 'plugin',
40 'filter' => 'handler',
44 'relationship' => 'handler',
52 * Returns the views data service.
54 * @return \Drupal\views\ViewsData
55 * Returns a views data cache object.
57 public static function viewsData() {
58 return \Drupal::service('views.views_data');
62 * Returns the views data helper service.
64 * @return \Drupal\views\ViewsDataHelper
65 * Returns a views data helper object.
67 public static function viewsDataHelper() {
68 return \Drupal::service('views.views_data_helper');
72 * Returns the view executable factory service.
74 * @return \Drupal\views\ViewExecutableFactory
75 * Returns a views executable factory.
77 public static function executableFactory() {
78 return \Drupal::service('views.executable');
82 * Returns the view analyzer.
84 * @return \Drupal\views\Analyzer
85 * Returns a view analyzer object.
87 public static function analyzer() {
88 return \Drupal::service('views.analyzer');
92 * Returns the plugin manager for a certain views plugin type.
95 * The plugin type, for example filter.
97 * @return \Drupal\views\Plugin\ViewsPluginManager
99 public static function pluginManager($type) {
100 return \Drupal::service('plugin.manager.views.' . $type);
104 * Returns the plugin manager for a certain views handler type.
106 * @return \Drupal\views\Plugin\ViewsHandlerManager
108 public static function handlerManager($type) {
109 return \Drupal::service('plugin.manager.views.' . $type);
113 * Loads a view from configuration and returns its executable object.
116 * The view ID to load.
118 * @return \Drupal\views\ViewExecutable
119 * A view executable instance, from the loaded entity.
121 public static function getView($id) {
122 $view = \Drupal::service('entity.manager')->getStorage('view')->load($id);
124 return static::executableFactory()->get($view);
129 * Fetches a list of all base tables available
131 * @param string $type
132 * Either 'display', 'style' or 'row'.
134 * For style plugins, this is an optional type to restrict to. May be
135 * 'normal', 'summary', 'feed' or others based on the needs of the display.
137 * An array of possible base tables.
140 * A keyed array of in the form of 'base_table' => 'Description'.
142 public static function fetchPluginNames($type, $key = NULL, array $base = []) {
143 $definitions = static::pluginManager($type)->getDefinitions();
146 foreach ($definitions as $id => $plugin) {
147 // Skip plugins that don't conform to our key, if they have one.
148 if ($key && isset($plugin['display_types']) && !in_array($key, $plugin['display_types'])) {
152 if (empty($plugin['no_ui']) && (empty($base) || empty($plugin['base']) || array_intersect($base, $plugin['base']))) {
153 $plugins[$id] = $plugin['title'];
157 if (!empty($plugins)) {
166 * Gets all the views plugin definitions.
169 * An array of plugin definitions for all types.
171 public static function getPluginDefinitions() {
173 foreach (ViewExecutable::getPluginTypes() as $plugin_type) {
174 $plugins[$plugin_type] = static::pluginManager($plugin_type)->getDefinitions();
181 * Gets enabled display extenders.
183 public static function getEnabledDisplayExtenders() {
184 $enabled = array_filter((array) \Drupal::config('views.settings')->get('display_extenders'));
186 return array_combine($enabled, $enabled);
190 * Return a list of all view IDs and display IDs that have a particular
191 * setting in their display's plugin settings.
193 * @param string $type
194 * A flag from the display plugin definitions (e.g, 'uses_menu_links').
197 * A list of arrays containing the $view_id and $display_id.
200 * array($view_id, $display_id),
201 * array($view_id, $display_id),
205 public static function getApplicableViews($type) {
206 // Get all display plugins which provides the type.
207 $display_plugins = static::pluginManager('display')->getDefinitions();
210 foreach ($display_plugins as $id => $definition) {
211 if (!empty($definition[$type])) {
212 $plugin_ids[$id] = $id;
216 $entity_ids = \Drupal::entityQuery('view')
217 ->condition('status', TRUE)
218 ->condition("display.*.display_plugin", $plugin_ids, 'IN')
222 foreach (\Drupal::entityTypeManager()->getStorage('view')->loadMultiple($entity_ids) as $view) {
223 // Check each display to see if it meets the criteria and is enabled.
225 foreach ($view->get('display') as $id => $display) {
226 // If the key doesn't exist, enabled is assumed.
227 $enabled = !empty($display['display_options']['enabled']) || !array_key_exists('enabled', $display['display_options']);
229 if ($enabled && in_array($display['display_plugin'], $plugin_ids)) {
230 $result[] = [$view->id(), $id];
239 * Returns an array of all views as fully loaded $view objects.
241 * @return \Drupal\views\Entity\View[]
242 * An array of loaded view entities.
244 public static function getAllViews() {
245 return \Drupal::entityManager()->getStorage('view')->loadMultiple();
249 * Returns an array of all enabled views.
251 * @return \Drupal\views\Entity\View[]
252 * An array of loaded enabled view entities.
254 public static function getEnabledViews() {
255 $query = \Drupal::entityQuery('view')
256 ->condition('status', TRUE)
259 return \Drupal::entityManager()->getStorage('view')->loadMultiple($query);
263 * Returns an array of all disabled views.
265 * @return \Drupal\views\Entity\View[]
266 * An array of loaded disabled view entities.
268 public static function getDisabledViews() {
269 $query = \Drupal::entityQuery('view')
270 ->condition('status', FALSE)
273 return \Drupal::entityManager()->getStorage('view')->loadMultiple($query);
277 * Returns an array of view as options array, that can be used by select,
278 * checkboxes and radios as #options.
280 * @param bool $views_only
281 * If TRUE, only return views, not displays.
282 * @param string $filter
283 * Filters the views on status. Can either be 'all' (default), 'enabled' or
285 * @param mixed $exclude_view
286 * View or current display to exclude.
288 * - views object (containing $exclude_view->storage->name and $exclude_view->current_display)
289 * - views name as string: e.g. my_view
290 * - views name and display id (separated by ':'): e.g. my_view:default
291 * @param bool $optgroup
292 * If TRUE, returns an array with optgroups for each view (will be ignored for
293 * $views_only = TRUE). Can be used by select
295 * If TRUE, the list of views is sorted ascending.
298 * An associative array for use in select.
299 * - key: view name and display id separated by ':', or the view name only.
301 public static function getViewsAsOptions($views_only = FALSE, $filter = 'all', $exclude_view = NULL, $optgroup = FALSE, $sort = FALSE) {
303 // Filter the big views array.
308 $filter = ucfirst($filter);
309 $views = call_user_func("static::get{$filter}Views");
315 // Prepare exclude view strings for comparison.
316 if (empty($exclude_view)) {
317 $exclude_view_name = '';
318 $exclude_view_display = '';
320 elseif (is_object($exclude_view)) {
321 $exclude_view_name = $exclude_view->storage->id();
322 $exclude_view_display = $exclude_view->current_display;
325 // Append a ':' to the $exclude_view string so we always have more than one
327 list($exclude_view_name, $exclude_view_display) = explode(':', "$exclude_view:");
331 foreach ($views as $view) {
333 // Return only views.
334 if ($views_only && $id != $exclude_view_name) {
335 $options[$id] = $view->label();
337 // Return views with display ids.
339 foreach ($view->get('display') as $display_id => $display) {
340 if (!($id == $exclude_view_name && $display_id == $exclude_view_display)) {
342 $options[$id][$id . ':' . $display['id']] = t('@view : @display', ['@view' => $id, '@display' => $display['id']]);
345 $options[$id . ':' . $display['id']] = t('View: @view - Display: @display', ['@view' => $id, '@display' => $display['id']]);
358 * Returns a list of plugins and metadata about them.
361 * An array keyed by PLUGIN_TYPE:PLUGIN_NAME, like 'display:page' or
362 * 'pager:full', containing an array with the following keys:
363 * - title: The plugin's title.
364 * - type: The plugin type.
365 * - module: The module providing the plugin.
366 * - views: An array of enabled Views that are currently using this plugin,
367 * keyed by machine name.
369 public static function pluginList() {
370 $plugin_data = static::getPluginDefinitions();
372 foreach (static::getEnabledViews() as $view) {
373 foreach ($view->get('display') as $display) {
374 foreach ($plugin_data as $type => $info) {
375 if ($type == 'display' && isset($display['display_plugin'])) {
376 $name = $display['display_plugin'];
378 elseif (isset($display['display_options']["{$type}_plugin"])) {
379 $name = $display['display_options']["{$type}_plugin"];
381 elseif (isset($display['display_options'][$type]['type'])) {
382 $name = $display['display_options'][$type]['type'];
388 // Key first by the plugin type, then the name.
389 $key = $type . ':' . $name;
390 // Add info for this plugin.
391 if (!isset($plugins[$key])) {
394 'title' => $info[$name]['title'],
395 'provider' => $info[$name]['provider'],
400 // Add this view to the list for this plugin.
401 $plugins[$key]['views'][$view->id()] = $view->id();
409 * Provide a list of views handler types used in a view, with some information
413 * An array of associative arrays containing:
414 * - title: The title of the handler type.
415 * - ltitle: The lowercase title of the handler type.
416 * - stitle: A singular title of the handler type.
417 * - lstitle: A singular lowercase title of the handler type.
418 * - plural: Plural version of the handler type.
419 * - (optional) type: The actual internal used handler type. This key is
420 * just used for header,footer,empty to link to the internal type: area.
422 public static function getHandlerTypes() {
423 // Statically cache this so translation only occurs once per request for all
425 if (!isset(static::$handlerTypes)) {
426 static::$handlerTypes = [
429 'title' => static::t('Fields'),
430 // Lowercase title for mid-sentence.
431 'ltitle' => static::t('fields'),
433 'stitle' => static::t('Field'),
434 // Singular lowercase title for mid sentence
435 'lstitle' => static::t('field'),
436 'plural' => 'fields',
439 'title' => static::t('Contextual filters'),
440 'ltitle' => static::t('contextual filters'),
441 'stitle' => static::t('Contextual filter'),
442 'lstitle' => static::t('contextual filter'),
443 'plural' => 'arguments',
446 'title' => static::t('Sort criteria'),
447 'ltitle' => static::t('sort criteria'),
448 'stitle' => static::t('Sort criterion'),
449 'lstitle' => static::t('sort criterion'),
453 'title' => static::t('Filter criteria'),
454 'ltitle' => static::t('filter criteria'),
455 'stitle' => static::t('Filter criterion'),
456 'lstitle' => static::t('filter criterion'),
457 'plural' => 'filters',
460 'title' => static::t('Relationships'),
461 'ltitle' => static::t('relationships'),
462 'stitle' => static::t('Relationship'),
463 'lstitle' => static::t('Relationship'),
464 'plural' => 'relationships',
467 'title' => static::t('Header'),
468 'ltitle' => static::t('header'),
469 'stitle' => static::t('Header'),
470 'lstitle' => static::t('Header'),
471 'plural' => 'header',
475 'title' => static::t('Footer'),
476 'ltitle' => static::t('footer'),
477 'stitle' => static::t('Footer'),
478 'lstitle' => static::t('Footer'),
479 'plural' => 'footer',
483 'title' => static::t('No results behavior'),
484 'ltitle' => static::t('no results behavior'),
485 'stitle' => static::t('No results behavior'),
486 'lstitle' => static::t('No results behavior'),
493 return static::$handlerTypes;
497 * Returns a list of plugin types.
499 * @param string $type
500 * (optional) filter the list of plugins by type. Available options are
501 * 'plugin' or 'handler'.
504 * An array of plugin types.
506 public static function getPluginTypes($type = NULL) {
507 if ($type === NULL) {
508 return array_keys(static::$plugins);
511 if (!in_array($type, ['plugin', 'handler'])) {
512 throw new \Exception('Invalid plugin type used. Valid types are "plugin" or "handler".');
515 return array_keys(array_filter(static::$plugins, function ($plugin_type) use ($type) {
516 return $plugin_type == $type;
521 * Translates a string to the current language or to a given language.
523 * See the t() documentation for details.
525 protected static function t($string, array $args = [], array $options = []) {
526 if (empty(static::$translationManager)) {
527 static::$translationManager = \Drupal::service('string_translation');
530 return static::$translationManager->translate($string, $args, $options);