class ViewExecutable (View source)

Represents a view as a whole.

An object to contain all of the data to generate a view, plus the member functions to build the view query, execute the query and render the output.

This class does not implement the Serializable interface since problems occurred when using the serialize method.

Properties

View $storage

The config entity in which the view is stored.

bool $built

Whether or not the view has been built.

bool $executed

Whether the view has been executed/query has been run.

array $args

Any arguments that have been passed into the view.

array $build_info

An array of build info.

protected bool $ajaxEnabled

Whether this view uses AJAX.

ResultRow[] $result

Where the results of a query will go.

protected int $current_page

The current page. If the view uses pagination.

protected int $items_per_page

The number of items per page.

protected int $offset

The pager offset.

int $total_rows

The total number of rows returned from the query.

array $attachment_before

Attachments to place before the view.

array $attachment_after

Attachments to place after the view.

array $feedIcons

Feed icons attached to the view.

array $exposed_data

All the form data from $form_state->getValues().

protected array $exposed_input

An array of input values from exposed forms.

array $exposed_raw_input

Exposed widget input directly from the $form_state->getValues().

ViewExecutable[] $old_view

Used to store views that were previously running if we recurse.

ViewExecutable[] $parent_views

To avoid recursion in views embedded into areas.

bool $is_attachment

Whether this view is an attachment to another view.

string $current_display

Identifier of the current display.

QueryPluginBase $query

Where the $query object will reside.

PagerPluginBase $pager

The used pager plugin used by the current executed view.

DisplayPluginBase $display_handler

The current used display plugin.

DisplayPluginCollection $displayHandlers

The list of used displays of the view.

StylePluginBase $style_plugin

The current used style plugin.

RowPluginBase $rowPlugin

The current used row plugin, if the style plugin supports row plugins.

int $row_index

Stores the current active row while rendering.

Url $override_url

Allow to override the url of the current view.

string $override_path

Allow to override the path used for generated urls.

bool $base_database

Allow to override the used database which is used for this query.

FieldPluginBase[] $field

Stores the field handlers which are initialized on this view.

ArgumentPluginBase[] $argument

Stores the argument handlers which are initialized on this view.

SortPluginBase[] $sort

Stores the sort handlers which are initialized on this view.

FilterPluginBase[] $filter

Stores the filter handlers which are initialized on this view.

RelationshipPluginBase[] $relationship

Stores the relationship handlers which are initialized on this view.

AreaPluginBase[] $header

Stores the area handlers for the header which are initialized on this view.

$footer

Stores the area handlers for the footer which are initialized on this view.

AreaPluginBase[] $empty

Stores the area handlers for the empty text which are initialized on this view.

protected Response $response

Stores the current response object.

protected Request $request

Stores the current request object.

bool $inited

Does this view already have loaded its handlers.

string $exposed_widgets

The rendered output of the exposed form.

bool $preview

If this view has been previewed.

bool $get_total_rows

Force the query to calculate the total number of results.

bool $build_sort

Indicates if the sorts have been built.

array $many_to_one_tables

Stores the many-to-one tables for performance.

string $dom_id

A unique identifier which allows to update multiple views output via js.

array $element

A render array container to store render related information.

protected AccountInterface $user

The current user.

$showAdminLinks

Should the admin links be shown on the rendered view.

protected ViewsData $viewsData

The views data.

protected RouteProviderInterface $routeProvider

The route provider.

protected EntityTypeInterface|false $baseEntityType

The entity type of the base table, if available.

protected array $serializationData

Holds all necessary data for proper unserialization.

Methods

__construct(ViewEntityInterface $storage, AccountInterface $user, ViewsData $views_data, RouteProviderInterface $route_provider)

Constructs a new ViewExecutable object.

string|null
id()

Returns the identifier.

save()

Saves the view.

setArguments(array $args)

Sets the arguments for the view.

$this
addCacheContext(string $cache_context)

Expands the list of used cache contexts for the view.

setCurrentPage(int $page)

Sets the current page for the pager.

int
getCurrentPage()

Gets the current page from the pager.

int
getItemsPerPage()

Gets the items per page from the pager.

setItemsPerPage(int $items_per_page)

Sets the items per page on the pager.

int
getOffset()

Gets the pager offset from the pager.

setOffset(int $offset)

Sets the offset on the pager.

bool
usePager()

Determines if the view uses a pager.

setAjaxEnabled(bool $ajax_enabled)

Sets whether or not AJAX should be used.

bool
ajaxEnabled()

Determines whether or not AJAX should be used.

setExposedInput(string[] $filters)

Sets the exposed filters input to an array.

string[]
getExposedInput()

Figures out what the exposed input for this view is.

true
initDisplay()

Sets the display for this view and initializes the display handler.

string
chooseDisplay(array|string $displays)

Gets the first display that is accessible to the user.

getDisplay()

Gets the current display plugin.

bool
setDisplay(string $display_id = NULL)

Sets the current display.

newDisplay(string $plugin_id = 'page', string $title = NULL, string $id = NULL)

Creates a new display and a display handler instance for it.

getStyle()

Gets the current style plugin.

bool
initStyle()

Finds and initializes the style plugin.

initHandlers()

Acquires and attaches all of the handlers.

getPager()

Gets the current pager plugin.

initPager()

Initializes the pager.

array|string
renderPager(string[] $exposed_input)

Renders the pager, if necessary.

array
getBaseTables()

Creates a list of base tables to be used by the view.

EntityType|false
getBaseEntityType()

Returns the entity type of the base table, if available.

_preQuery()

Runs the preQuery() on all active handlers.

_postExecute()

Runs the postExecute() on all active handlers.

_initHandler(string $key, array $info)

Attaches the views handler for the specific type.

bool
_buildArguments()

Builds all the arguments.

getQuery()

Gets the current query plugin.

true
initQuery()

Initializes the query object for the view.

bool|null
build(string $display_id = NULL)

Builds the query for the view.

_build(string $key)

Builds an individual set of handlers.

bool
execute(string $display_id = NULL)

Executes the view's query.

array|null
render(string $display_id = NULL)

Renders this view for a certain display.

string[]
getCacheTags()

Gets the cache tags associated with the executed view.

array|null
buildRenderable(string $display_id = NULL, array $args = [], bool $cache = TRUE)

Builds the render array outline for the given display.

array|null
executeDisplay(string $display_id = NULL, string[] $args = [])

Executes the given display, with the given arguments.

array|null
preview($display_id = NULL, $args = [])

Previews the given display, with the given arguments.

preExecute(array $args = [])

Runs attachments and lets the display do what it needs to before running.

postExecute()

Unsets the current view, mostly.

attachDisplays()

Runs attachment displays for the view.

bool
access(string $displays = NULL, AccountInterface $account = NULL)

Determines if the given user has access to the view.

setResponse(Response $response)

Sets the used response object of the view.

Response
getResponse()

Gets the response object used by the view.

setRequest(Request $request)

Sets the request object.

Request
getRequest()

Gets the request object.

string|false
getTitle()

Gets the view's current title.

true
setTitle($title)

Overrides the view's current title.

buildTitle()

Forces the view to build a title.

bool
hasUrl(array $args = NULL, string $display_id = NULL)

Determines whether you can link to the view or a particular display.

Url
getUrl(array $args = NULL, string $display_id = NULL)

Gets the URL for the current view.

Url
getUrlInfo(string $display_id = '')

Gets the Url object associated with the display handler.

string|false
getPath()

Gets the base path used for this view.

getUser()

Gets the current user.

createDuplicate()

Creates a duplicate ViewExecutable object.

destroy()

Unsets references so that a $view object may be properly garbage collected.

array
validate()

Makes sure the view is completely valid.

static array
getHandlerTypes()

Provides a list of views handler types used in a view.

static array
getPluginTypes($type = NULL)

Returns the valid types of plugins that can be used.

string
addHandler(string $display_id, string $type, string $table, string $field, array $options = [], string $id = NULL)

Adds an instance of a handler to the view.

static string
generateHandlerId(string $requested_id, array $existing_items)

Generates a unique ID for a handler instance.

array
getHandlers(string $type, string $display_id = NULL)

Gets an array of handler instances for the current display.

array|null
getHandler(string $display_id, string $type, string $id)

Gets the configuration of a handler instance on a given display.

setHandler(string $display_id, string $type, string $id, array|null $item)

Sets the configuration of a handler instance on a given display.

removeHandler(string $display_id, string $type, string $id)

Removes configuration for a handler instance on a given display.

setHandlerOption(string $display_id, string $type, string $id, string $option, mixed $value)

Sets an option on a handler instance.

setShowAdminLinks(bool $show_admin_links)

Enables admin links on the rendered view.

bool
getShowAdminLinks()

Returns whether admin links should be rendered on the view.

mergeDefaults()

Merges all plugin default values for each display.

array
buildThemeFunctions(string $hook)

Provides a full array of possible theme functions to try for a given hook.

bool
hasFormElements()

Determines if this view has form elements.

array
getDependencies()

Gets dependencies for the view.

array
__sleep()

Magic method implementation to serialize the view executable.

__wakeup()

Magic method implementation to unserialize the view executable.

Details

__construct(ViewEntityInterface $storage, AccountInterface $user, ViewsData $views_data, RouteProviderInterface $route_provider)

Constructs a new ViewExecutable object.

Parameters

ViewEntityInterface $storage

The view config entity the actual information is stored on.

AccountInterface $user

The current user.

ViewsData $views_data

The views data.

RouteProviderInterface $route_provider

The route provider.

string|null id()

Returns the identifier.

Return Value

string|null

The entity identifier, or NULL if the object does not yet have an identifier.

save()

Saves the view.

setArguments(array $args)

Sets the arguments for the view.

Parameters

array $args

The arguments passed to the view.

$this addCacheContext(string $cache_context)

Expands the list of used cache contexts for the view.

Parameters

string $cache_context

The additional cache context.

Return Value

$this

setCurrentPage(int $page)

Sets the current page for the pager.

Parameters

int $page

The current page.

int getCurrentPage()

Gets the current page from the pager.

Return Value

int

The current page.

int getItemsPerPage()

Gets the items per page from the pager.

Return Value

int

The items per page.

setItemsPerPage(int $items_per_page)

Sets the items per page on the pager.

Parameters

int $items_per_page

The items per page.

int getOffset()

Gets the pager offset from the pager.

Return Value

int

The pager offset.

setOffset(int $offset)

Sets the offset on the pager.

Parameters

int $offset

The pager offset.

bool usePager()

Determines if the view uses a pager.

Return Value

bool

TRUE if the view uses a pager, FALSE otherwise.

setAjaxEnabled(bool $ajax_enabled)

Sets whether or not AJAX should be used.

If AJAX is used, paging, table sorting, and exposed filters will be fetched via an AJAX call rather than a page refresh.

Parameters

bool $ajax_enabled

TRUE if AJAX should be used, FALSE otherwise.

bool ajaxEnabled()

Determines whether or not AJAX should be used.

Return Value

bool

TRUE if AJAX is enabled, FALSE otherwise.

setExposedInput(string[] $filters)

Sets the exposed filters input to an array.

Parameters

string[] $filters

The values taken from the view's exposed filters and sorts.

string[] getExposedInput()

Figures out what the exposed input for this view is.

They will be taken from \Drupal::request()->query or from something previously set on the view.

Return Value

string[]

An array containing the exposed input values keyed by the filter and sort name.

See also

\Drupal\views\self::setExposedInput()

true initDisplay()

Sets the display for this view and initializes the display handler.

Return Value

true

Always returns TRUE.

string chooseDisplay(array|string $displays)

Gets the first display that is accessible to the user.

Parameters

array|string $displays

Either a single display id or an array of display ids.

Return Value

string

The first accessible display id, at least default.

DisplayPluginBase getDisplay()

Gets the current display plugin.

Return Value

DisplayPluginBase

The current display plugin.

bool setDisplay(string $display_id = NULL)

Sets the current display.

Parameters

string $display_id

The ID of the display to mark as current.

Return Value

bool

TRUE if the display was correctly set, FALSE otherwise.

DisplayPluginBase newDisplay(string $plugin_id = 'page', string $title = NULL, string $id = NULL)

Creates a new display and a display handler instance for it.

Parameters

string $plugin_id

(optional) The plugin type from the Views plugin annotation. Defaults to 'page'.

string $title

(optional) The title of the display. Defaults to NULL.

string $id

(optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults to NULL.

Return Value

DisplayPluginBase

A new display plugin instance if executable is set, the new display ID otherwise.

StylePluginBase getStyle()

Gets the current style plugin.

Return Value

StylePluginBase

The current style plugin.

bool initStyle()

Finds and initializes the style plugin.

Note that arguments may have changed which style plugin we use, so check the view object first, then ask the display handler.

Return Value

bool

TRUE if the style plugin was or could be initialized, FALSE otherwise.

initHandlers()

Acquires and attaches all of the handlers.

PagerPluginBase getPager()

Gets the current pager plugin.

Return Value

PagerPluginBase

The current pager plugin.

initPager()

Initializes the pager.

Like style initialization, pager initialization is held until late to allow for overrides.

array|string renderPager(string[] $exposed_input)

Renders the pager, if necessary.

Parameters

string[] $exposed_input

The input values from the exposed forms and sorts of the view.

Return Value

array|string

The render array of the pager if it's set, blank string otherwise.

array getBaseTables()

Creates a list of base tables to be used by the view.

This is used primarily for the UI. The display must be already initialized.

Return Value

array

An array of base tables to be used by the view.

EntityType|false getBaseEntityType()

Returns the entity type of the base table, if available.

Return Value

EntityType|false

The entity type of the base table, or FALSE if none exists.

protected _preQuery()

Runs the preQuery() on all active handlers.

protected _postExecute()

Runs the postExecute() on all active handlers.

protected _initHandler(string $key, array $info)

Attaches the views handler for the specific type.

Parameters

string $key

One of 'argument', 'field', 'sort', 'filter', 'relationship'.

array $info

An array of views handler types use in the view with additional information about them.

protected bool _buildArguments()

Builds all the arguments.

Return Value

bool

TRUE if the arguments were built successfully, FALSE otherwise.

QueryPluginBase getQuery()

Gets the current query plugin.

Return Value

QueryPluginBase

The current query plugin.

true initQuery()

Initializes the query object for the view.

Return Value

true

Always returns TRUE.

bool|null build(string $display_id = NULL)

Builds the query for the view.

Parameters

string $display_id

The display ID of the view.

Return Value

bool|null

TRUE if the view build process was successful, FALSE if setting the display fails or NULL if the view has been built already.

_build(string $key)

Builds an individual set of handlers.

This is an internal method.

Some filter needs this function, even it is internal.

Parameters

string $key

The type of handlers (filter etc.) which should be iterated over to build the relationship and query information.

bool execute(string $display_id = NULL)

Executes the view's query.

Parameters

string $display_id

The machine name of the display, which should be executed.

Return Value

bool

TRUE if the view execution was successful, FALSE otherwise. For example, an argument could stop the process.

array|null render(string $display_id = NULL)

Renders this view for a certain display.

Note: You should better use just the preview function if you want to render a view.

Parameters

string $display_id

The machine name of the display, which should be rendered.

Return Value

array|null

A renderable array containing the view output or NULL if the build process failed.

string[] getCacheTags()

Gets the cache tags associated with the executed view.

Note: The cache plugin controls the used tags, so you can override it, if needed.

Return Value

string[]

An array of cache tags.

array|null buildRenderable(string $display_id = NULL, array $args = [], bool $cache = TRUE)

Builds the render array outline for the given display.

This render array has a #pre_render callback which will call ::executeDisplay in order to actually execute the view and then build the final render array structure.

Parameters

string $display_id

The display ID.

array $args

An array of arguments passed along to the view.

bool $cache

(optional) Should the result be render cached.

Return Value

array|null

A renderable array with #type 'view' or NULL if the display ID was invalid.

array|null executeDisplay(string $display_id = NULL, string[] $args = [])

Executes the given display, with the given arguments.

To be called externally by whatever mechanism invokes the view, such as a page callback, hook_block, etc.

This function should NOT be used by anything external as this returns data in the format specified by the display. It can also have other side effects that are only intended for the 'proper' use of the display, such as setting page titles.

If you simply want to view the display, use View::preview() instead.

Parameters

string $display_id

The display ID of the view to be executed.

string[] $args

The arguments to be passed to the view.

Return Value

array|null

A renderable array containing the view output or NULL if the display ID of the view to be executed doesn't exist.

array|null preview($display_id = NULL, $args = [])

Previews the given display, with the given arguments.

To be called externally, probably by an AJAX handler of some flavor. Can also be called when views are embedded, as this guarantees normalized output.

This function does not do any access checks on the view. It is the responsibility of the caller to check $view->access() or implement other access logic. To render the view normally with access checks, use views_embed_view() instead.

Parameters

$display_id
$args

Return Value

array|null

A renderable array containing the view output or NULL if the display ID of the view to be executed doesn't exist.

preExecute(array $args = [])

Runs attachments and lets the display do what it needs to before running.

Parameters

array $args

An array of arguments from the URL that can be used by the view.

postExecute()

Unsets the current view, mostly.

attachDisplays()

Runs attachment displays for the view.

bool access(string $displays = NULL, AccountInterface $account = NULL)

Determines if the given user has access to the view.

Note that this sets the display handler if it hasn't been set.

Parameters

string $displays

The machine name of the display.

AccountInterface $account

The user object.

Return Value

bool

TRUE if the user has access to the view, FALSE otherwise.

setResponse(Response $response)

Sets the used response object of the view.

Parameters

Response $response

The response object which should be set.

Response getResponse()

Gets the response object used by the view.

Return Value

Response

The response object of the view.

setRequest(Request $request)

Sets the request object.

Parameters

Request $request

The request object.

Request getRequest()

Gets the request object.

Return Value

Request

The request object.

string|false getTitle()

Gets the view's current title.

This can change depending upon how it was built.

Return Value

string|false

The view title, FALSE if the display is not set.

true setTitle($title)

Overrides the view's current title.

The tokens in the title gets replaced before rendering.

Parameters

$title

Return Value

true

Always returns TRUE.

buildTitle()

Forces the view to build a title.

bool hasUrl(array $args = NULL, string $display_id = NULL)

Determines whether you can link to the view or a particular display.

Some displays (e.g. block displays) do not have their own route, but may optionally provide a link to another display that does have a route.

Parameters

array $args

(optional) The arguments.

string $display_id

(optional) The display ID. The current display will be used by default.

Return Value

bool

TRUE if the current display has a valid route available, FALSE otherwise.

Url getUrl(array $args = NULL, string $display_id = NULL)

Gets the URL for the current view.

This URL will be adjusted for arguments.

Parameters

array $args

(optional) Passed in arguments.

string $display_id

(optional) Specify the display ID to link to, fallback to the current ID.

Return Value

Url

The URL of the current view.

Exceptions

InvalidArgumentException

Url getUrlInfo(string $display_id = '')

Gets the Url object associated with the display handler.

Parameters

string $display_id

(optional) The display ID (used only to detail an exception).

Return Value

Url

The display handlers URL object.

Exceptions

InvalidArgumentException

string|false getPath()

Gets the base path used for this view.

Return Value

string|false

The base path used for the view or FALSE if setting the display fails.

AccountInterface getUser()

Gets the current user.

Views plugins can receive the current user in order to not need dependency injection.

Return Value

AccountInterface

The current user.

createDuplicate()

Creates a duplicate ViewExecutable object.

Makes a copy of this view that has been sanitized of handlers, any runtime data, ID, and UUID.

destroy()

Unsets references so that a $view object may be properly garbage collected.

array validate()

Makes sure the view is completely valid.

Return Value

array

An array of error strings. This will be empty if there are no validation errors.

static array getHandlerTypes()

Provides a list of views handler types used in a view.

This also provides some information about the views handler types.

Return Value

array

An array of associative arrays containing:

  • title: The title of the handler type.
  • ltitle: The lowercase title of the handler type.
  • stitle: A singular title of the handler type.
  • lstitle: A singular lowercase title of the handler type.
  • plural: Plural version of the handler type.
  • (optional) type: The actual internal used handler type. This key is just used for header,footer,empty to link to the internal type: area.

static array getPluginTypes($type = NULL)

Returns the valid types of plugins that can be used.

Parameters

$type

Return Value

array

An array of plugin type strings.

string addHandler(string $display_id, string $type, string $table, string $field, array $options = [], string $id = NULL)

Adds an instance of a handler to the view.

Items may be fields, filters, sort criteria, or arguments.

Parameters

string $display_id

The machine name of the display.

string $type

The type of handler being added.

string $table

The name of the table this handler is from.

string $field

The name of the field this handler is from.

array $options

(optional) Extra options for this instance. Defaults to an empty array.

string $id

(optional) A unique ID for this handler instance. Defaults to NULL, in which case one will be generated.

Return Value

string

The unique ID for this handler instance.

static string generateHandlerId(string $requested_id, array $existing_items)

Generates a unique ID for a handler instance.

These handler instances are typically fields, filters, sort criteria, or arguments.

Parameters

string $requested_id

The requested ID for the handler instance.

array $existing_items

An array of existing handler instances, keyed by their IDs.

Return Value

string

A unique ID. This will be equal to $requested_id if no handler instance with that ID already exists. Otherwise, it will be appended with an integer to make it unique, e.g., "{$requested_id}_1", "{$requested_id}_2", etc.

array getHandlers(string $type, string $display_id = NULL)

Gets an array of handler instances for the current display.

Parameters

string $type

The type of handlers to retrieve.

string $display_id

(optional) A specific display machine name to use. If NULL, the current display will be used.

Return Value

array

An array of handler instances of a given type for this display.

array|null getHandler(string $display_id, string $type, string $id)

Gets the configuration of a handler instance on a given display.

Parameters

string $display_id

The machine name of the display.

string $type

The type of handler to retrieve.

string $id

The ID of the handler to retrieve.

Return Value

array|null

Either the handler instance's configuration, or NULL if the handler is not used on the display.

setHandler(string $display_id, string $type, string $id, array|null $item)

Sets the configuration of a handler instance on a given display.

Parameters

string $display_id

The machine name of the display.

string $type

The type of handler being set.

string $id

The ID of the handler being set.

array|null $item

An array of configuration for a handler, or NULL to remove this instance.

See also

set_item_option()

removeHandler(string $display_id, string $type, string $id)

Removes configuration for a handler instance on a given display.

Parameters

string $display_id

The machine name of the display.

string $type

The type of handler being removed.

string $id

The ID of the handler being removed.

setHandlerOption(string $display_id, string $type, string $id, string $option, mixed $value)

Sets an option on a handler instance.

Use this only if you have just 1 or 2 options to set; if you have many, consider getting the handler instance, adding the options and using set_item() directly.

Parameters

string $display_id

The machine name of the display.

string $type

The type of handler being set.

string $id

The ID of the handler being set.

string $option

The configuration key for the value being set.

mixed $value

The value being set.

See also

set_item()

Enables admin links on the rendered view.

Parameters

bool $show_admin_links

TRUE if the admin links should be shown.

Returns whether admin links should be rendered on the view.

Return Value

bool

TRUE if admin links should be rendered, else FALSE.

mergeDefaults()

Merges all plugin default values for each display.

array buildThemeFunctions(string $hook)

Provides a full array of possible theme functions to try for a given hook.

Parameters

string $hook

The hook to use. This is the base theme/template name.

Return Value

array

An array of theme hook suggestions.

bool hasFormElements()

Determines if this view has form elements.

Return Value

bool

TRUE if this view contains handlers with views form implementations, FALSE otherwise.

array getDependencies()

Gets dependencies for the view.

Return Value

array

An array of dependencies grouped by type (module, theme, entity).

See also

View::calculateDependencies
\Drupal\views\Entity\View::getDependencies()

array __sleep()

Magic method implementation to serialize the view executable.

Return Value

array

The names of all variables that should be serialized.

__wakeup()

Magic method implementation to unserialize the view executable.