cms/drupal/modules/simpletest/tests/taxonomy_test.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Test module for Taxonomy hooks and functions not used in core.
       
     6  *
       
     7  * @see TaxonomyHooksTestCase::testTaxonomyTermHooks()
       
     8  */
       
     9 
       
    10 /**
       
    11  * Implements hook_taxonomy_term_load().
       
    12  */
       
    13 function taxonomy_test_taxonomy_term_load($terms) {
       
    14   foreach ($terms as $term) {
       
    15     $antonym = taxonomy_test_get_antonym($term->tid);
       
    16     if ($antonym) {
       
    17       $term->antonym = $antonym;
       
    18     }
       
    19   }
       
    20 }
       
    21 
       
    22 /**
       
    23  * Implements hook_taxonomy_term_insert().
       
    24  */
       
    25 function taxonomy_test_taxonomy_term_insert($term) {
       
    26   if (!empty($term->antonym)) {
       
    27     db_insert('taxonomy_term_antonym')
       
    28       ->fields(array(
       
    29         'tid' => $term->tid,
       
    30         'name' => trim($term->antonym)
       
    31       ))
       
    32       ->execute();
       
    33   }
       
    34 }
       
    35 
       
    36 /**
       
    37  * Implements hook_taxonomy_term_update().
       
    38  */
       
    39 function taxonomy_test_taxonomy_term_update($term) {
       
    40   if (!empty($term->antonym)) {
       
    41     db_merge('taxonomy_term_antonym')
       
    42       ->key(array('tid' => $term->tid))
       
    43       ->fields(array(
       
    44         'name' => trim($term->antonym)
       
    45       ))
       
    46       ->execute();
       
    47   }
       
    48 }
       
    49 
       
    50 /**
       
    51  * Implements hook_taxonomy_term_delete().
       
    52  */
       
    53 function taxonomy_test_taxonomy_term_delete($term) {
       
    54   db_delete('taxonomy_term_antonym')
       
    55     ->condition('tid', $term->tid)
       
    56     ->execute();
       
    57 }
       
    58 
       
    59 /**
       
    60  * Implements hook_taxonomy_term_view().
       
    61  */
       
    62 function taxonomy_test_taxonomy_term_view($term, $view_mode, $langcode) {
       
    63   if ($view_mode == 'full') {
       
    64     $term->content['taxonomy_test_term_view_check'] = array(
       
    65       '#prefix' => '<div>',
       
    66       '#markup' => t('The antonym is %antonym', array('%antonym' => $term->antonym)),
       
    67       '#suffix' => '</div>',
       
    68       '#weight' => 10,
       
    69     );
       
    70   }
       
    71 }
       
    72 
       
    73 /**
       
    74  * Implements hook_entity_view().
       
    75  */
       
    76 function taxonomy_test_entity_view($entity, $type, $view_mode, $langcode) {
       
    77   if ($type == 'taxonomy_term' && $view_mode == 'full') {
       
    78     $entity->content['taxonomy_test_entity_view_check'] = array(
       
    79       '#prefix' => '<div>',
       
    80       '#markup' => t('The antonym is %antonym', array('%antonym' => $entity->antonym)),
       
    81       '#suffix' => '</div>',
       
    82       '#weight' => 20,
       
    83     );
       
    84   }
       
    85 }
       
    86 
       
    87 /**
       
    88  * Implements hook_form_alter().
       
    89  */
       
    90 function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
       
    91   if ($form_id == 'taxonomy_form_term') {
       
    92     $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
       
    93     $form['advanced']['antonym'] = array(
       
    94       '#type' => 'textfield',
       
    95       '#title' => t('Antonym'),
       
    96       '#default_value' => !empty($antonym) ? $antonym : '',
       
    97       '#description' => t('Antonym of this term.')
       
    98     );
       
    99   }
       
   100 }
       
   101 
       
   102 /**
       
   103  * Return the antonym of the given term ID.
       
   104  */
       
   105 function taxonomy_test_get_antonym($tid) {
       
   106   return db_select('taxonomy_term_antonym', 'ta')
       
   107     ->fields('ta', array('name'))
       
   108     ->condition('tid', $tid)
       
   109     ->execute()
       
   110     ->fetchField();
       
   111 }
       
   112 
       
   113 /**
       
   114  * Implements hook_query_alter().
       
   115  */
       
   116 function taxonomy_test_query_alter(QueryAlterableInterface $query) {
       
   117   $value = variable_get(__FUNCTION__);
       
   118   if (isset($value)) {
       
   119     variable_set(__FUNCTION__, ++$value);
       
   120   }
       
   121 }
       
   122 
       
   123 /**
       
   124  * Implements hook_query_TAG_alter().
       
   125  */
       
   126 function taxonomy_test_query_term_access_alter(QueryAlterableInterface $query) {
       
   127   $value = variable_get(__FUNCTION__);
       
   128   if (isset($value)) {
       
   129     variable_set(__FUNCTION__, ++$value);
       
   130   }
       
   131 }
       
   132 
       
   133 /**
       
   134  * Implements hook_query_TAG_alter().
       
   135  */
       
   136 function taxonomy_test_query_taxonomy_term_access_alter(QueryAlterableInterface $query) {
       
   137   $value = variable_get(__FUNCTION__);
       
   138   if (isset($value)) {
       
   139     variable_set(__FUNCTION__, ++$value);
       
   140   }
       
   141 }