Skip to content

ViewsHooks

Views hook implementations for the Helper module.


  • Full name: \Drupal\helper\Hook\ViewsHooks

Properties

entityType

protected \Drupal\helper\EntityType $entityType

logger

protected \Psr\Log\LoggerInterface $logger

theme

protected \Drupal\helper\Theme $theme

viewHelper

protected ?\Drupal\helper\ViewHelper $viewHelper

moderationInformation

protected ?\Drupal\content_moderation\ModerationInformationInterface $moderationInformation

Methods

__construct

Constructs a new ViewHooks object.

public __construct(\Drupal\helper\EntityType $entityType, \Psr\Log\LoggerInterface $logger, \Drupal\helper\Theme $theme, ?\Drupal\helper\ViewHelper $viewHelper = null, ?\Drupal\content_moderation\ModerationInformationInterface $moderationInformation = null): mixed

Parameters:

Parameter Type Description
$entityType \Drupal\helper\EntityType
$logger \Psr\Log\LoggerInterface
$theme \Drupal\helper\Theme
$viewHelper ?\Drupal\helper\ViewHelper
$moderationInformation ?\Drupal\content_moderation\ModerationInformationInterface

alterViewsForm

Implements hook_form_alter().

public alterViewsForm(mixed& $form, \Drupal\Core\Form\FormStateInterface $form_state, mixed $form_id): void

Parameters:

Parameter Type Description
$form mixed
$form_state \Drupal\Core\Form\FormStateInterface
$form_id mixed

alterViewsExposedForm

Implements hook_form_FORM_ID_alter() for views_exposed_form.

public alterViewsExposedForm(array& $form, \Drupal\Core\Form\FormStateInterface $form_state, string $form_id): void

Parameters:

Parameter Type Description
$form array
$form_state \Drupal\Core\Form\FormStateInterface
$form_id string

addFieldBundleStates

Shows exposed field filters only for the bundles that have the field.

protected addFieldBundleStates(array& $form, \Drupal\Core\Form\FormStateInterface $form_state, \Drupal\views\ViewExecutable $view): void

When the view has an exposed bundle filter for its base entity type, each exposed filter on a field that some of the offered bundles don't have gets #states that show it only when the bundle filter is set to any bundle, or to a bundle with the field. The #states go on the filter's wrapper when it has one, so an exposed operator is hidden with it. If none of the bundles the view is filtered to have the field, the filter is also rendered hidden, so it doesn't show until #states hides it. Those bundles come from all of the view's bundle filters, not only the exposed one.

Taxonomy term filters that are not on one field, such as Taxonomy Entity Index's "Has taxonomy terms (with depth)", are shown for the bundles with any field that can reference the filter's vocabulary.

Filters with a relationship, filters that already have #states, and the dependent filters of Views Dependent Filters, which adds its #states once the form is built, are not changed. A multiple select bundle filter needs the core_states_conditions_fix helper; without it, no #states are added and an error is logged.

Parameters:

Parameter Type Description
$form array
The exposed form.
$form_state \Drupal\Core\Form\FormStateInterface
The form state.
$view \Drupal\views\ViewExecutable
The view.

See Also:

  • \Drupal\helper\Hook\static::getBundleConditions()

findBundleFilter

Finds the exposed bundle filter that field filters depend on.

protected findBundleFilter(\Drupal\views\ViewExecutable $view, array $form): array|null

Parameters:

Parameter Type Description
$view \Drupal\views\ViewExecutable
The view.
$form array
The exposed form.

Return Value:

NULL if the view has no exposed bundle filter for users to choose bundles with. Otherwise what ViewHelper::getExposedBundleFilter() returns, plus: - element: The filter's form element, or an empty array if it isn't found. Better Exposed Filters can show it as checkboxes or radios. - ids: All of the view's bundle filters, keyed by filter ID, which are not field filters.

See Also:

  • \Drupal\helper\ViewHelper::getExposedBundleFilter()

getViewsDependentFilterIdentifiers

Returns the identifiers of Views Dependent Filters' dependent filters.

protected static getViewsDependentFilterIdentifiers(\Drupal\Core\Form\FormStateInterface $form_state): string[]
  • This method is static. Parameters:
Parameter Type Description
$form_state \Drupal\Core\Form\FormStateInterface
The exposed form's state.

Return Value:

The dependent filters' identifiers. Empty if the view has none.

See Also:

  • \Drupal\views_dependent_filters\Plugin\views\filter\ViewsDependentFilter::buildExposedForm()

getBundleConditions

Builds the #states conditions for the bundles that have a field.

protected getBundleConditions(string $identifier, bool $multiple, array $bundleElement, array $fieldBundles): array

The conditions match when the bundle filter is set to any bundle, or to a bundle that has the field: - For a select or radios, its value is "All" (- Any -) or one of the bundles. For a multiple select, nothing is selected or one of the selected bundles has the field, which needs the core_states_conditions_fix helper. - For checkboxes, such as from Better Exposed Filters, no checkbox is checked or the checkbox of a bundle with the field is. These are core #states conditions.

Parameters:

Parameter Type Description
$identifier string
The bundle filter's identifier, such as "type".
$multiple bool
Whether the bundle filter allows several bundles.
$bundleElement array
The bundle filter's form element, or an empty array if it isn't found.
$fieldBundles array
The bundles that have the field.

Return Value:

The conditions for the 'visible' state.


afterBuildRemoveDependentSelfStates

Form #after_build callback for views exposed forms.

public static afterBuildRemoveDependentSelfStates(array $form, \Drupal\Core\Form\FormStateInterface $form_state): array

Views Dependent Filters keeps a dependent filter visible while it has a value, by adding #states conditions on the filter's own input. Then #states never hides it, so it is never reset. This removes those conditions, so a dependent filter is only shown for its controlling filters' values.

If a filter only has conditions on its own input, they are kept, since removing them would always show it.

  • This method is static. Parameters:
Parameter Type Description
$form array
The exposed form.
$form_state \Drupal\Core\Form\FormStateInterface
The form state.

Return Value:

The exposed form.

See Also:

  • \Drupal\helper\Hook\views_dependent_filters_exposed_form_after_build()

isConditionOnInput

Checks if a #states condition is on an input.

protected static isConditionOnInput(array $condition, string $identifier): bool
  • This method is static. Parameters:
Parameter Type Description
$condition array
The condition, keyed by the selectors it's on.
$identifier string
The input's name, such as an exposed filter's identifier. Inputs with
multiple values, named like "$identifier[...]", match too.

Return Value:

TRUE if any of the condition's selectors is on the input.


afterBuildHideEmptyChoices

Form #after_build callback for views exposed forms.

public static afterBuildHideEmptyChoices(array $element): array

Hides selects, radios and checkboxes that have nothing to choose. Ones nested in other elements, like a filter's wrapper with its operator, are also hidden. The wrapper itself stays visible.

  • This method is static. Parameters:
Parameter Type Description
$element array
The form, or an element in it.

Return Value:

The element.

See Also:

  • \Drupal\helper\Hook\static::isEmptyChoice()

isEmptyChoice

Checks if an exposed filter's choice element has nothing to choose.

protected static isEmptyChoice(array $element): bool

The element is a select, or radios or checkboxes, such as the widgets of Better Exposed Filters. It has nothing to choose when: - It has no options besides "All" (- Any -), so there is nothing to filter by. - It is a single select or radios without "All" and has only one option, such as a required filter or an operator with one choice. It always uses that option.

A multiple select or checkboxes with one option are kept, since checking it or not still changes the results. Options in option groups are counted, not the groups.

  • This method is static. Parameters:
Parameter Type Description
$element array
The select, radios or checkboxes element.

Return Value:

TRUE if the element has nothing to choose.


alterViewsCachePlugins

Implements hook_views_plugins_cache_alter().

public alterViewsCachePlugins(array& $plugins): mixed

Swap the core 'tag' cache plugin class with a custom one.

Parameters:

Parameter Type Description
$plugins array

filterBulkActionOptions

Filter the entity bulk action options.

protected filterBulkActionOptions(array& $actionOptions, \Drupal\views\ViewExecutable $view): void

Removes the delete action when the user can't delete any listed bundle, the publish actions when every listed bundle is moderated, and the sticky and promote actions when no listed bundle shows the sticky or promote field in its default form display.

Parameters:

Parameter Type Description
$actionOptions array
The entity bulk action options.
$view \Drupal\views\ViewExecutable
The view.

See Also:

  • https://www.drupal.org/project/drupal/issues/2895262

theme

Implements hook_theme().

public theme(): array

preprocessViewsViewTable

Implements hook_preprocess_views_view_table().

public preprocessViewsViewTable(array& $variables): void

Core gives the sort indicator of a table's active column the direction the next click sorts in. Claro and Default Admin, which replaces it in Drupal 11.5, draw that as an arrow, so a column sorted in ascending order shows a descending arrow. Themes based on core's tablesort CSS, such as Stark and Olivero, draw the opposite arrow for each direction, so they are already right. The helper's setting lists the themes to change, along with their sub-themes, and defaults to Claro and Default Admin.

The active column uses the tablesort_indicator__current template instead, which shows the opposite arrow, the direction the column is sorted in now. Its text for screen readers still says what clicking will do.

Parameters:

Parameter Type Description
$variables array

See Also:

  • https://www.drupal.org/project/drupal/issues/2853130