hook_field_attach_form

Versions
7
hook_field_attach_form($obj_type, $object, &$form, &$form_state, $langcode)

Act on field_attach_form.

This hook is invoked after the field module has performed the operation.

See field_attach_form() for details and arguments.

Related topics

Code

modules/field/field.api.php, line 878

<?php
function hook_field_attach_form($obj_type, $object, &$form, &$form_state, $langcode) {
  $tids = array();

  // Collect every possible term attached to any of the fieldable entities.
  foreach ($objects as $id => $object) {
    foreach ($items[$id] as $delta => $item) {
      // Force the array key to prevent duplicates.
      $tids[$item['value']] = $item['value'];
    }
  }
  if ($tids) {
    $terms = array();

    // Avoid calling taxonomy_term_load_multiple because it could lead to
    // circular references.
    $query = db_select('taxonomy_term_data', 't');
    $query->fields('t');
    $query->condition('t.tid', $tids, 'IN');
    $query->addTag('term_access');
    $terms = $query->execute()->fetchAllAssoc('tid');

    // Iterate through the fieldable entities again to attach the loaded term data.
    foreach ($objects as $id => $object) {
      foreach ($items[$id] as $delta => $item) {
        // Check whether the taxonomy term field instance value could be loaded.
        if (isset($terms[$item['value']])) {
          // Replace the instance value with the term data.
          $items[$id][$delta]['taxonomy_term'] = $terms[$item['value']];
        }
        // Otherwise, unset the instance value, since the term does not exist.
        else {
          unset($items[$id][$delta]);
        }
      }
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.