0

I have a multi-valued image field that gets populated automatically with several images per entity. I need to add a manual "approval" step where one or more of the images is selected to be used later. What is a clean way to implement the approval process for existing images?

  • You want approval for entire node or approval per field ? Is approval kind of display / don't display image to end users who would like to view images ? – Anil Sagar May 5 '14 at 16:30
  • 1
    Approval per image. Exactly - choose whether to show an image to end users but this will feed a custom resource for services. I'm looking at field_collection as a way to add a boolean to each image field. – Joe Beuckman May 5 '14 at 16:39
  • 1
    Yes, Field collection is the way to go.. – Anil Sagar May 6 '14 at 2:30
0

Use field_collection module to build a field that has an image and a boolean.

Copy existing multi-image fields into the new field_collection like this:

/**
 * Copy a multi-image field into a multi-field_collection with an image field
 *
 * This function requires a patch for field_collection 7.x-1.0-beta7 like this:
 * wget https://drupal.org/files/1937866-field_collection-metadata-setter.patch
 * patch -p1 < 1937866-field_collection-metadata-setter.patch
 *
 * @param $nid  The node to update
 * @param $multi_image_source Name of the existing multiple-images field
 * @param $dest_field_collection Name of the new field_collection that will get the photos
 * @param $dest_field Name of the image field inside the field_collection
 */
function _copy_images_to_selectable_field($nid, $multi_image_source, $dest_field_collection, $dest_field) {

  $entity = entity_load_single('node', $nid);
  $wrapper = entity_metadata_wrapper('node', $entity);

  if ($wrapper->$dest_field_collection->value() != null) {
    drupal_set_message("skipping node with existing destination field ".$nid);
    return;
  }

  $photos = $wrapper->$multi_image_source->value();

  $collections = array();

  foreach ($photos as $photo) {

    $fc = entity_create('field_collection_item', array('field_name' => $dest_field_collection, 'revision_id' => 1));
    $fc->setHostEntity('node', $entity);

    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc);

    $fc_wrapper->$dest_field->set($photo);

    $fc->save(TRUE);
    $collections[] = $fc;
  }

  $wrapper->$dest_field_collection->set($collections);

  $wrapper->save();
}

**NOTE: This function requires a patch for field_collection 7.x-1.0-beta7. Download and apply the patch from the field_collection module's folder like this:

wget https://drupal.org/files/1937866-field_collection-metadata-setter.patch
patch -p1 < 1937866-field_collection-metadata-setter.patch
|improve this answer|||||

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.