You are here

function imagefield_tokens_field_attach_presave in ImageField Tokens 7

Implements hook_field_attach_presave().

File

./imagefield_tokens.module, line 191
Implements image Alt and Title field defaults with support for tokens.

Code

function imagefield_tokens_field_attach_presave($entity_type, $entity) {

  // Includes interation code from _field_invoke().
  $options = array(
    'default' => FALSE,
    'deleted' => FALSE,
    'language' => NULL,
  );

  // Get list of image fields.
  $image_fields = array();
  foreach (field_info_fields() as $field) {
    if ($field['type'] == 'image') {
      $image_fields[] = $field['field_name'];
    }
  }

  // Determine the list of instances to iterate on.
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
  foreach ($instances as $instance) {
    if (in_array($instance['field_name'], $image_fields)) {
      $field_name = $instance['field_name'];
      $field = field_info_field($field_name);

      // Determine the list of languages to iterate on.
      $available_languages = field_available_languages($entity_type, $field);
      $languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
      foreach ($languages as $langcode) {
        if (isset($entity->{$field_name}[$langcode])) {
          foreach ($entity->{$field_name}[$langcode] as &$field_data) {
            $settings = $instance['settings'];

            // If ("Update every edit" or title is empty)
            // and a default is specified.
            if (($settings['title_field_update_on_edit'] || empty($field_data['title'])) && !empty($settings['title_field_default'])) {
              $field_data['title'] = $settings['title_field_default'];
            }
            if (isset($field_data['title'])) {
              $field_data['title'] = token_replace($field_data['title'], array(
                $entity_type => $entity,
              ), array(
                'sanitize' => TRUE,
                'clear' => TRUE,
              ));
            }

            // If Alt should equal title field, else same as above.
            if ($settings['alt_field_sync_title'] && isset($field_data['title'])) {
              $field_data['alt'] = $field_data['title'];
            }
            else {
              if (($settings['alt_field_update_on_edit'] || empty($field_data['alt'])) && !empty($settings['alt_field_default'])) {
                $field_data['alt'] = $settings['alt_field_default'];
              }
              if (isset($field_data['alt'])) {
                $field_data['alt'] = token_replace($field_data['alt'], array(
                  $entity_type => $entity,
                ), array(
                  'sanitize' => TRUE,
                  'clear' => TRUE,
                ));
              }
            }

            // Truncate fields to max allowed length.
            // alt=512, title=1024 per image.install.
            if (isset($field_data['title'])) {
              $field_data['title'] = truncate_utf8($field_data['title'], 1024, TRUE, TRUE);
              $field_data['title'] = htmlspecialchars_decode($field_data['title'], ENT_QUOTES | ENT_HTML401);
            }
            if (isset($field_data['alt'])) {
              $field_data['alt'] = truncate_utf8($field_data['alt'], 512, TRUE, TRUE);
              $field_data['alt'] = htmlspecialchars_decode($field_data['alt'], ENT_QUOTES | ENT_HTML401);
            }
          }
        }
      }
    }
  }
}