taxonomy_test.module

Same filename in other branches
  1. 7.x modules/simpletest/tests/taxonomy_test.module
  2. 9 core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  3. 8.9.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  4. 10 core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module

File

core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module

View source
<?php


/**
 * @file
 * Provides test hook implementations for taxonomy tests.
 */
declare (strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\taxonomy\TermInterface;

/**
 * Implements hook_entity_access().
 */
function taxonomy_test_entity_access(EntityInterface $entity, string $operation, AccountInterface $account) : AccessResultInterface {
    if ($entity instanceof TermInterface) {
        $parts = explode(' ', (string) $entity->label());
        if (in_array('Inaccessible', $parts, TRUE) && in_array($operation, $parts, TRUE)) {
            return AccessResult::forbidden();
        }
    }
    return AccessResult::neutral();
}

/**
 * Implements hook_query_alter().
 */
function taxonomy_test_query_alter(AlterableInterface $query) {
    $value = \Drupal::state()->get(__FUNCTION__);
    if (isset($value)) {
        \Drupal::state()->set(__FUNCTION__, ++$value);
    }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_term_access_alter(AlterableInterface $query) {
    $value = \Drupal::state()->get(__FUNCTION__);
    if (isset($value)) {
        \Drupal::state()->set(__FUNCTION__, ++$value);
    }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_taxonomy_term_access_alter(AlterableInterface $query) {
    $value = \Drupal::state()->get(__FUNCTION__);
    if (isset($value)) {
        \Drupal::state()->set(__FUNCTION__, ++$value);
    }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for the taxonomy term form.
 */
function taxonomy_test_form_taxonomy_term_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    if (\Drupal::state()->get('taxonomy_test.disable_parent_form_element', FALSE)) {
        $form['relations']['parent']['#disabled'] = TRUE;
    }
}

/**
 * Implements hook_ENTITY_TYPE_load() for the taxonomy term.
 */
function taxonomy_test_taxonomy_term_load($entities) {
    $value = \Drupal::state()->get(__FUNCTION__);
    // Only record loaded terms is the test has set this to an empty array.
    if (is_array($value)) {
        $value = array_merge($value, array_keys($entities));
        \Drupal::state()->set(__FUNCTION__, array_unique($value));
    }
}

Functions

Title Deprecated Summary
taxonomy_test_entity_access Implements hook_entity_access().
taxonomy_test_form_taxonomy_term_form_alter Implements hook_form_BASE_FORM_ID_alter() for the taxonomy term form.
taxonomy_test_query_alter Implements hook_query_alter().
taxonomy_test_query_taxonomy_term_access_alter Implements hook_query_TAG_alter().
taxonomy_test_query_term_access_alter Implements hook_query_TAG_alter().
taxonomy_test_taxonomy_term_load Implements hook_ENTITY_TYPE_load() for the taxonomy term.

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