1. 8.5.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  2. 8.2.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  3. 8.3.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  4. 8.4.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  5. 8.6.x core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
  6. 7.x modules/simpletest/tests/taxonomy_test.module

Test module for Taxonomy hooks and functions not used in core.

See also

TaxonomyHooksTestCase::testTaxonomyTermHooks()

File

modules/simpletest/tests/taxonomy_test.module
View source
<?php

/**
 * @file
 * Test module for Taxonomy hooks and functions not used in core.
 *
 * @see TaxonomyHooksTestCase::testTaxonomyTermHooks()
 */

/**
 * Implements hook_taxonomy_term_load().
 */
function taxonomy_test_taxonomy_term_load($terms) {
  foreach ($terms as $term) {
    $antonym = taxonomy_test_get_antonym($term->tid);
    if ($antonym) {
      $term->antonym = $antonym;
    }
  }
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function taxonomy_test_taxonomy_term_insert($term) {
  if (!empty($term->antonym)) {
    db_insert('taxonomy_term_antonym')
      ->fields(array(
      'tid' => $term->tid,
      'name' => trim($term->antonym),
    ))
      ->execute();
  }
}

/**
 * Implements hook_taxonomy_term_update().
 */
function taxonomy_test_taxonomy_term_update($term) {
  if (!empty($term->antonym)) {
    db_merge('taxonomy_term_antonym')
      ->key(array(
      'tid' => $term->tid,
    ))
      ->fields(array(
      'name' => trim($term->antonym),
    ))
      ->execute();
  }
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function taxonomy_test_taxonomy_term_delete($term) {
  db_delete('taxonomy_term_antonym')
    ->condition('tid', $term->tid)
    ->execute();
}

/**
 * Implements hook_taxonomy_term_view().
 */
function taxonomy_test_taxonomy_term_view($term, $view_mode, $langcode) {
  if ($view_mode == 'full') {
    $term->content['taxonomy_test_term_view_check'] = array(
      '#prefix' => '<div>',
      '#markup' => t('The antonym is %antonym', array(
        '%antonym' => $term->antonym,
      )),
      '#suffix' => '</div>',
      '#weight' => 10,
    );
  }
}

/**
 * Implements hook_entity_view().
 */
function taxonomy_test_entity_view($entity, $type, $view_mode, $langcode) {
  if ($type == 'taxonomy_term' && $view_mode == 'full') {
    $entity->content['taxonomy_test_entity_view_check'] = array(
      '#prefix' => '<div>',
      '#markup' => t('The antonym is %antonym', array(
        '%antonym' => $entity->antonym,
      )),
      '#suffix' => '</div>',
      '#weight' => 20,
    );
  }
}

/**
 * Implements hook_form_alter().
 */
function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'taxonomy_form_term') {
    $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
    $form['advanced']['antonym'] = array(
      '#type' => 'textfield',
      '#title' => t('Antonym'),
      '#default_value' => !empty($antonym) ? $antonym : '',
      '#description' => t('Antonym of this term.'),
    );
  }
}

/**
 * Return the antonym of the given term ID.
 */
function taxonomy_test_get_antonym($tid) {
  return db_select('taxonomy_term_antonym', 'ta')
    ->fields('ta', array(
    'name',
  ))
    ->condition('tid', $tid)
    ->execute()
    ->fetchField();
}

/**
 * Implements hook_query_alter().
 */
function taxonomy_test_query_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_term_access_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_taxonomy_term_access_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}

Functions