Skip to content

ViewHelper

Helpers for working with Views.


  • Full name: \Drupal\helper\ViewHelper

Properties

entityTypeHelper

protected \Drupal\helper\EntityType $entityTypeHelper

entityFieldManager

protected \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager

Methods

__construct

Constructs a new ViewHelper object.

public __construct(\Drupal\helper\EntityType $entityTypeHelper, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager): mixed

Parameters:

Parameter Type Description
$entityTypeHelper \Drupal\helper\EntityType
$entityFieldManager \Drupal\Core\Entity\EntityFieldManagerInterface

getExposedBundleFilter

Returns the view's exposed bundle filter that users choose bundles with.

public getExposedBundleFilter(\Drupal\views\ViewExecutable $view): array|null

This is the first of the view's bundle filters that is exposed without an exposed operator.

Parameters:

Parameter Type Description
$view \Drupal\views\ViewExecutable
View executable object.

Return Value:

NULL if the view has no such filter, or it offers no bundles. Otherwise an array with: - id: The filter ID. - identifier: The filter's identifier in the exposed form, such as "type". - multiple: Whether several bundles can be chosen. - bundles: The bundle IDs it offers, as strings. A filter limited to its selected bundles ("reduce") only offers those.

See Also:

  • \Drupal\helper\static::getBundleFilters()

getFilterFieldBundles

Returns the bundles that have the field a filter is on.

public getFilterFieldBundles(\Drupal\views\Plugin\views\filter\FilterPluginBase $filter): array|null

Parameters:

Parameter Type Description
$filter \Drupal\views\Plugin\views\filter\FilterPluginBase
The filter, which must be initialized for a view.

Return Value:

The bundle IDs of the filter's entity type that have the field. For a taxonomy term filter that isn't on one field, such as Taxonomy Entity Index's "Has taxonomy terms (with depth)", the bundles with a field that can reference the filter's vocabulary. NULL if the filter isn't on a field in the field map, and isn't such a term filter.


getBundleFilters

Returns the view's filters on the bundle of its base entity type.

public getBundleFilters(\Drupal\views\ViewExecutable $view): array[]

A bundle filter uses the 'bundle' filter plugin, or filters on the base entity type's bundle field. Filters with a relationship are skipped, since they filter other entities.

Parameters:

Parameter Type Description
$view \Drupal\views\ViewExecutable
View executable object.

Return Value:

The bundle filters' configuration, keyed by filter ID, in the order of the view's filters. Empty if the view has no base entity type or no bundle filter.


getEntityBundlesFromFilter

Retrieves the entity bundle from the filter configuration for the view.

public getEntityBundlesFromFilter(\Drupal\views\ViewExecutable $view): string[]|null

This method iterates over the view's filter handlers to locate a handler named 'bundle' and extracts its value, if available.

Note: this finds the first filter that matches the criteria.

Parameters:

Parameter Type Description
$view \Drupal\views\ViewExecutable
View executable object.

Return Value:

A list of the entity bundles retrieved from the filter configuration, or NULL if the view is not filtered by bundle. For filters using the "not in" operator, this is the remaining bundles of the base entity type, which may be an empty array if all bundles are excluded.


hasRelationshipToEntityType

Checks if the view has a relationship to entities of an entity type.

public hasRelationshipToEntityType(\Drupal\views\ViewExecutable $view, string $entityTypeId): bool

The view must be built first, for example with ViewExecutable::build(), since the relationships are only added to the query when it is built.

Parameters:

Parameter Type Description
$view \Drupal\views\ViewExecutable
View executable object.
$entityTypeId string
The entity type ID.

Return Value:

TRUE if the view joins entities of the entity type with a relationship, otherwise FALSE.