function Views::getApplicableViews

Same name in other branches
  1. 9 core/modules/views/src/Views.php \Drupal\views\Views::getApplicableViews()
  2. 10 core/modules/views/src/Views.php \Drupal\views\Views::getApplicableViews()
  3. 11.x core/modules/views/src/Views.php \Drupal\views\Views::getApplicableViews()

Return a list of all view IDs and display IDs that have a particular setting in their display's plugin settings.

array(
    array(
        $view_id,
        $display_id,
    ),
    array(
        $view_id,
        $display_id,
    ),
);

Parameters

string $type: A flag from the display plugin definitions (e.g, 'uses_menu_links').

Return value

array A list of arrays containing the $view_id and $display_id.

5 calls to Views::getApplicableViews()
RouteSubscriber::getApplicableViews in core/modules/views/src/EventSubscriber/RouteSubscriber.php
Returns all views/display combinations with routes.
ViewsLocalTask::getApplicableMenuViews in core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php
Return a list of all views and display IDs that have a menu entry.
ViewsMenuLink::getDerivativeDefinitions in core/modules/views/src/Plugin/Derivative/ViewsMenuLink.php
Gets the definition of all derivatives of a base plugin.
ViewsSelection::buildConfigurationForm in core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
Form constructor.
ViewsTest::testGetApplicableViews in core/modules/views/tests/src/Unit/ViewsTest.php
@covers ::getApplicableViews

File

core/modules/views/src/Views.php, line 206

Class

Views
Static service container wrapper for views.

Namespace

Drupal\views

Code

public static function getApplicableViews($type) {
    // Get all display plugins which provides the type.
    $display_plugins = static::pluginManager('display')->getDefinitions();
    $plugin_ids = [];
    foreach ($display_plugins as $id => $definition) {
        if (!empty($definition[$type])) {
            $plugin_ids[$id] = $id;
        }
    }
    $entity_ids = \Drupal::entityQuery('view')->condition('status', TRUE)
        ->condition("display.*.display_plugin", $plugin_ids, 'IN')
        ->execute();
    $result = [];
    foreach (\Drupal::entityTypeManager()->getStorage('view')
        ->loadMultiple($entity_ids) as $view) {
        // Check each display to see if it meets the criteria and is enabled.
        foreach ($view->get('display') as $id => $display) {
            // If the key doesn't exist, enabled is assumed.
            $enabled = !empty($display['display_options']['enabled']) || !array_key_exists('enabled', $display['display_options']);
            if ($enabled && in_array($display['display_plugin'], $plugin_ids)) {
                $result[] = [
                    $view->id(),
                    $id,
                ];
            }
        }
    }
    return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.