function views_add_contextual_links

Same name and namespace in other branches
  1. 11.x core/modules/views/views.module \views_add_contextual_links()
  2. 10 core/modules/views/views.module \views_add_contextual_links()
  3. 9 core/modules/views/views.module \views_add_contextual_links()
  4. 8.9.x core/modules/views/views.module \views_add_contextual_links()

Adds contextual links associated with a view display to a renderable array.

This function should be called when a view is being rendered in a particular location and you want to attach the appropriate contextual links (e.g., links for editing the view) to it.

The function operates by checking the view's display plugin to see if it has defined any contextual links that are intended to be displayed in the requested location; if so, it attaches them. The contextual links intended for a particular location are defined by the 'contextual links' and 'contextual_links_locations' properties in the plugin annotation; as a result, these hook implementations have full control over where and how contextual links are rendered for each display.

In addition to attaching the contextual links to the passed-in array (via the standard #contextual_links property), this function also attaches additional information via the #views_contextual_links_info property. This stores an array whose keys are the names of each module that provided views-related contextual links (same as the keys of the #contextual_links array itself) and whose values are themselves arrays whose keys ('location', 'view_name', and 'view_display_id') store the location, name of the view, and display ID that were passed in to this function. This allows you to access information about the contextual links and how they were generated in a variety of contexts where you might be manipulating the renderable array later on (for example, alter hooks which run later during the same page request).

Parameters

array $render_element: The renderable array to which contextual links will be added. This array should be suitable for passing in to \Drupal\Core\Render\RendererInterface::render() and will normally contain a representation of the view display whose contextual links are being requested.

string $location: The location in which the calling function intends to render the view and its contextual links. The core system supports three options for this parameter:

  • 'block': Used when rendering a block which contains a view. This retrieves any contextual links intended to be attached to the block itself.
  • 'page': Used when rendering the main content of a page which contains a view. This retrieves any contextual links intended to be attached to the page itself (for example, links which are displayed directly next to the page title).
  • 'view': Used when rendering the view itself, in any context. This retrieves any contextual links intended to be attached directly to the view.

If you are rendering a view and its contextual links in another location, you can pass in a different value for this parameter. However, you will also need to set 'contextual_links_locations' in your plugin annotation to indicate which view displays support having their contextual links rendered in the location you have defined.

string $display_id: The ID of the display within $view whose contextual links will be added.

array $view_element: The render array of the view. It should contain the following properties:

  • #view_id: The ID of the view.
  • #view_display_show_admin_links: A boolean whether the admin links should be shown.
  • #view_display_plugin_id: The plugin ID of the display.

Deprecated

in drupal:11.4.0 and is removed from drupal:13.0.0. Use Drupal\views\ContextualLinksHelper::addLinks() instead.

See also

https://www.drupal.org/node/3382344

File

core/modules/views/views.module, line 80

Code

function views_add_contextual_links(&$render_element, $location, $display_id, ?array $view_element = NULL) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use Drupal\\views\\ContextualLinksHelper::addLinks() instead. See https://www.drupal.org/node/3382344', E_USER_DEPRECATED);
  \Drupal::service(ContextualLinksHelper::class)->addLinks($render_element, $location, $display_id, $view_element);
}

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