function Views::getViewsAsOptions

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

Returns an array of view as options array.

This array can be used by select, checkboxes and radios as #options.

Parameters

bool $views_only: If TRUE, only return views, not displays.

string $filter: Filters the views on status. Can either be 'all' (default), 'enabled' or 'disabled'

\Drupal\views\ViewExecutable|string $exclude_view: View or current display to exclude. Either a:

  • Views executable object
  • views name, for example 'my_view'
  • views name and display ID separated by ':', for example 'my_view:page'

bool $optgroup: If TRUE, returns an array with optgroups for each view (will be ignored for $views_only = TRUE). Can be used by select

bool $sort: If TRUE, the list of views is sorted ascending.

Return value

array An associative array for use in select.

  • key: view name and display ID separated by ':', or the view name only.
2 calls to Views::getViewsAsOptions()
ModuleTest::testLoadFunctions in core/modules/views/tests/src/Kernel/ModuleTest.php
Tests the load wrapper/helper functions.
View::buildOptionsForm in core/modules/views/src/Plugin/views/area/View.php
Provide a form to edit options for this plugin.

File

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

Class

Views
Static service container wrapper for views.

Namespace

Drupal\views

Code

public static function getViewsAsOptions($views_only = FALSE, $filter = 'all', $exclude_view = NULL, $optgroup = FALSE, $sort = FALSE) {
    // Filter the big views array.
    switch ($filter) {
        case 'all':
        case 'disabled':
        case 'enabled':
            $filter = ucfirst($filter);
            $views = call_user_func(static::class . "::get{$filter}Views");
            break;
        default:
            return [];
    }
    // Prepare exclude view strings for comparison.
    if (empty($exclude_view)) {
        $exclude_view_name = '';
        $exclude_view_display = '';
    }
    elseif (is_object($exclude_view)) {
        $exclude_view_name = $exclude_view->storage
            ->id();
        $exclude_view_display = $exclude_view->current_display;
    }
    else {
        // Append a ':' to the $exclude_view string so we always have more than one
        // item to explode.
        [
            $exclude_view_name,
            $exclude_view_display,
        ] = explode(':', "{$exclude_view}:");
    }
    $options = [];
    foreach ($views as $view) {
        $id = $view->id();
        // Return only views.
        if ($views_only && $id != $exclude_view_name) {
            $options[$id] = $view->label();
        }
        else {
            foreach ($view->get('display') as $display_id => $display) {
                if (!($id == $exclude_view_name && $display_id == $exclude_view_display)) {
                    if ($optgroup) {
                        $options[$id][$id . ':' . $display['id']] = t('@view : @display', [
                            '@view' => $id,
                            '@display' => $display['id'],
                        ]);
                    }
                    else {
                        $options[$id . ':' . $display['id']] = t('View: @view - Display: @display', [
                            '@view' => $id,
                            '@display' => $display['id'],
                        ]);
                    }
                }
            }
        }
    }
    if ($sort) {
        ksort($options);
    }
    return $options;
}

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