cms/drupal/modules/taxonomy/taxonomy.api.php
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Hooks provided by the Taxonomy module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * @addtogroup hooks
       
    10  * @{
       
    11  */
       
    12 
       
    13 /**
       
    14  * Act on taxonomy vocabularies when loaded.
       
    15  *
       
    16  * Modules implementing this hook can act on the vocabulary objects before they
       
    17  * are returned by taxonomy_vocabulary_load_multiple().
       
    18  *
       
    19  * @param $vocabulary
       
    20  *   An array of taxonomy vocabulary objects.
       
    21  */
       
    22 function hook_taxonomy_vocabulary_load($vocabularies) {
       
    23   $result = db_select('mytable', 'm')
       
    24     ->fields('m', array('vid', 'foo'))
       
    25     ->condition('m.vid', array_keys($vocabularies), 'IN')
       
    26     ->execute();
       
    27   foreach ($result as $record) {
       
    28     $vocabularies[$record->vid]->foo = $record->foo;
       
    29   }
       
    30 }
       
    31 
       
    32 /**
       
    33  * Act on taxonomy vocabularies before they are saved.
       
    34  *
       
    35  * Modules implementing this hook can act on the vocabulary object before it is
       
    36  * inserted or updated.
       
    37  *
       
    38  * @param $vocabulary
       
    39  *   A taxonomy vocabulary object.
       
    40  */
       
    41 function hook_taxonomy_vocabulary_presave($vocabulary) {
       
    42   $vocabulary->foo = 'bar';
       
    43 }
       
    44 
       
    45 /**
       
    46  * Act on taxonomy vocabularies when inserted.
       
    47  *
       
    48  * Modules implementing this hook can act on the vocabulary object when saved
       
    49  * to the database.
       
    50  *
       
    51  * @param $vocabulary
       
    52  *   A taxonomy vocabulary object.
       
    53  */
       
    54 function hook_taxonomy_vocabulary_insert($vocabulary) {
       
    55   if ($vocabulary->machine_name == 'my_vocabulary') {
       
    56     $vocabulary->weight = 100;
       
    57   }
       
    58 }
       
    59 
       
    60 /**
       
    61  * Act on taxonomy vocabularies when updated.
       
    62  *
       
    63  * Modules implementing this hook can act on the vocabulary object when updated.
       
    64  *
       
    65  * @param $vocabulary
       
    66  *   A taxonomy vocabulary object.
       
    67  */
       
    68 function hook_taxonomy_vocabulary_update($vocabulary) {
       
    69   db_update('mytable')
       
    70     ->fields(array('foo' => $vocabulary->foo))
       
    71     ->condition('vid', $vocabulary->vid)
       
    72     ->execute();
       
    73 }
       
    74 
       
    75 /**
       
    76  * Respond to the deletion of taxonomy vocabularies.
       
    77  *
       
    78  * Modules implementing this hook can respond to the deletion of taxonomy
       
    79  * vocabularies from the database.
       
    80  *
       
    81  * @param $vocabulary
       
    82  *   A taxonomy vocabulary object.
       
    83  */
       
    84 function hook_taxonomy_vocabulary_delete($vocabulary) {
       
    85   db_delete('mytable')
       
    86     ->condition('vid', $vocabulary->vid)
       
    87     ->execute();
       
    88 }
       
    89 
       
    90 /**
       
    91  * Act on taxonomy terms when loaded.
       
    92  *
       
    93  * Modules implementing this hook can act on the term objects returned by
       
    94  * taxonomy_term_load_multiple().
       
    95  *
       
    96  * For performance reasons, information to be added to term objects should be
       
    97  * loaded in a single query for all terms where possible.
       
    98  *
       
    99  * Since terms are stored and retrieved from cache during a page request, avoid
       
   100  * altering properties provided by the {taxonomy_term_data} table, since this
       
   101  * may affect the way results are loaded from cache in subsequent calls.
       
   102  *
       
   103  * @param $terms
       
   104  *   An array of term objects, indexed by tid.
       
   105  */
       
   106 function hook_taxonomy_term_load($terms) {
       
   107   $result = db_select('mytable', 'm')
       
   108     ->fields('m', array('tid', 'foo'))
       
   109     ->condition('m.tid', array_keys($terms), 'IN')
       
   110     ->execute();
       
   111   foreach ($result as $record) {
       
   112     $terms[$record->tid]->foo = $record->foo;
       
   113   }
       
   114 }
       
   115 
       
   116 /**
       
   117  * Act on taxonomy terms before they are saved.
       
   118  *
       
   119  * Modules implementing this hook can act on the term object before it is
       
   120  * inserted or updated.
       
   121  *
       
   122  * @param $term
       
   123  *   A term object.
       
   124  */
       
   125 function hook_taxonomy_term_presave($term) {
       
   126   $term->foo = 'bar';
       
   127 }
       
   128 
       
   129 /**
       
   130  * Act on taxonomy terms when inserted.
       
   131  *
       
   132  * Modules implementing this hook can act on the term object when saved to
       
   133  * the database.
       
   134  *
       
   135  * @param $term
       
   136  *   A taxonomy term object.
       
   137  */
       
   138 function hook_taxonomy_term_insert($term) {
       
   139   db_insert('mytable')
       
   140     ->fields(array(
       
   141       'tid' => $term->tid,
       
   142       'foo' => $term->foo,
       
   143     ))
       
   144     ->execute();
       
   145 }
       
   146 
       
   147 /**
       
   148  * Act on taxonomy terms when updated.
       
   149  *
       
   150  * Modules implementing this hook can act on the term object when updated.
       
   151  *
       
   152  * @param $term
       
   153  *   A taxonomy term object.
       
   154  */
       
   155 function hook_taxonomy_term_update($term) {
       
   156   db_update('mytable')
       
   157     ->fields(array('foo' => $term->foo))
       
   158     ->condition('tid', $term->tid)
       
   159     ->execute();
       
   160 }
       
   161 
       
   162 /**
       
   163  * Respond to the deletion of taxonomy terms.
       
   164  *
       
   165  * Modules implementing this hook can respond to the deletion of taxonomy
       
   166  * terms from the database.
       
   167  *
       
   168  * @param $term
       
   169  *   A taxonomy term object.
       
   170  */
       
   171 function hook_taxonomy_term_delete($term) {
       
   172   db_delete('mytable')
       
   173     ->condition('tid', $term->tid)
       
   174     ->execute();
       
   175 }
       
   176 
       
   177 /**
       
   178  * Act on a taxonomy term that is being assembled before rendering.
       
   179  *
       
   180  * The module may add elements to $term->content prior to rendering. The
       
   181  * structure of $term->content is a renderable array as expected by
       
   182  * drupal_render().
       
   183  *
       
   184  * @param $term
       
   185  *   The term that is being assembled for rendering.
       
   186  * @param $view_mode
       
   187  *   The $view_mode parameter from taxonomy_term_view().
       
   188  * @param $langcode
       
   189  *   The language code used for rendering.
       
   190  *
       
   191  * @see hook_entity_view()
       
   192  */
       
   193 function hook_taxonomy_term_view($term, $view_mode, $langcode) {
       
   194   $term->content['my_additional_field'] = array(
       
   195     '#markup' => $additional_field,
       
   196     '#weight' => 10,
       
   197     '#theme' => 'mymodule_my_additional_field',
       
   198   );
       
   199 }
       
   200 
       
   201 /**
       
   202  * Alter the results of taxonomy_term_view().
       
   203  *
       
   204  * This hook is called after the content has been assembled in a structured
       
   205  * array and may be used for doing processing which requires that the complete
       
   206  * taxonomy term content structure has been built.
       
   207  *
       
   208  * If the module wishes to act on the rendered HTML of the term rather than the
       
   209  * structured content array, it may use this hook to add a #post_render
       
   210  * callback. Alternatively, it could also implement
       
   211  * hook_preprocess_taxonomy_term(). See drupal_render() and theme()
       
   212  * documentation respectively for details.
       
   213  *
       
   214  * @param $build
       
   215  *   A renderable array representing the term.
       
   216  *
       
   217  * @see hook_entity_view_alter()
       
   218  */
       
   219 function hook_taxonomy_term_view_alter(&$build) {
       
   220   if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
       
   221     // Change its weight.
       
   222     $build['an_additional_field']['#weight'] = -10;
       
   223   }
       
   224 
       
   225   // Add a #post_render callback to act on the rendered HTML of the term.
       
   226   $build['#post_render'][] = 'my_module_node_post_render';
       
   227 }
       
   228 
       
   229 /**
       
   230  * @} End of "addtogroup hooks".
       
   231  */