2

I have a case where I need to add three custom fields to each image in an image field to hold data. The content of these fields will not be displayed, but will only be indexed into Solr so they can be used in a custom image search. The logical place for these fields is in the same place that the Alt and Title fields are displayed (when enabled in the field settings), so that they can be entered in when the image is attached. Considering all the field API goodness in D7, it seemed like this would be pretty straightforward. However, it's not turning out that way.

One suggestion was to use the File Entity module, so I can add custom fields. This seems to work great, in that it allows me to add fields through the UI to the file entity. However, the problem is that these fields are not available when actually attaching the image to a node - they are only available when you add an image at /file/add.

After looking at the code in image.module, I noticed that the Alt and Title fields are added in the #process callback, which in turn is added in the hook_field_widget_form() imeplentation (image_field_widget_form()). Since there is an alter hook (hook_field_widget_form_alter()), I implemented it and added my own #process callback:

function nb_alters_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($element['#field_name'] == 'field_images') {
    // Add a custom #process function to add the fields.
    $element['#process'][] = 'nb_alters_image_process';
  }
}

and then added my callback (using code suggestion found here:

function nb_alters_image_process($element, &$form_state, $form) {
  if (isset($element['#type']) && ($info = element_info($element['#type'])) && isset($info['#process'])) {
    foreach ($info['#process'] as $process) {
      $process($element, $form_state);
    }
  }

  // Add the additional Person, and Organization fields.
  $element['person'] = array(
    '#title' => t('Person'),
    '#type' => 'textfield',
    //'#default_value' => isset($item['person']) ? $item['person'] : '',
    '#default_value' => t('Default Person text'),
    '#description' => t('The person'),
    // @see http://www.gawds.org/show.php?contentid=28
    '#maxlength' => 255,
    '#weight' => -2,
    //'#access' => (bool) $item['fid'] && $settings['alt_field'],
    '#access' => TRUE,
  );
  $element['organization'] = array(
    '#type' => 'textfield',
    '#title' => t('Organization'),
    //'#default_value' => isset($item['title']) ? $item['title'] : '',
    '#default_value' => t('Default Organization text'),
    '#description' => t('The organization the person in the image belongs to.'),
    '#maxlength' => 255,
    '#weight' => -1,
    //'#access' => (bool) $item['fid'] && $settings['title_field'],
    '#access' => TRUE,
  );

  return $element;
}

However, this doesn't work as expected. Instead of the fields being added to each image, they are added separately below the last uploaded image.

What do I need to do differently to get my fields to show up for each image? Am I on the right track?

Thanks.

  • 1
    FWIW, I ended up going with a Field Collection instead, but I'd still be curious to see if what I want to do is possible in code. – wonder95 Jul 3 '14 at 17:24
  • I have the exact same problem, except with file field. I need to add a checkbox "Do not publically display" so, if checked, those files are hidden from the node display. I'm assuming too you needed your fields to be part of each image fieldset when clicking "Add another" right? Can you add an answer that talks about how you achieved this with Field Collection? – blue928 Jul 22 '14 at 9:26
  • All I did was create a field collection that had my image field and three text fields for the meta data. Setting the number of allowed items to more than one just uses the built in functionality to add another item, which adds another set of fields defined in the field collection. – wonder95 Jul 23 '14 at 15:05
1

This question got me scratching my head also for a couple of hours, until I found File Entity Inline module.

Basically this module provides the functionality the OP wanted. You just create the field in the file entity you need to and then when creating the file/image field you have an additional box in the editing window where you can select the fields you want to be displayed next to each file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.