Let's say I have some image content types. When viewing one of those image content type instances, I would like to add the image to one of my galleries. Currently I am doing this by adding a link to the node--custom-type.tpl.php which links to a list of galleries from which I can choose to add the image. The feature request is to move this type of thing into the media_gallery itself, something like:

function media_gallery_get_add_to_gallery_link($fid, $link_text, $attrs) { 
  return an html link that content types can use;
}
function media_gallery_add_to_gallery_list($fid) {
  return table formatted list of galleries;
}
function media_gallery_add_to_gallery($gallery_id, $fid) {
  add file to gallery;
  return media_gallery_add_to_gallery_list($fid);
}

Here is some code that I quickly put together for this outside of media_gallery:

function site_configuration_menu() {
  $items['exhibit/add-to-list/%'] = array(
    'title' => 'Add image to exhibit',
    'access callback' => 'site_configuration_add_image_to_gallery_access',
    //'access arguments' => array('gallery', 2),
    'page callback' => 'site_configuration_add_image_to_gallery_form',
    'page arguments' => array(2),
  );

  $items['exhibit/add/%/%'] = array(
    'title' => 'Add image to exhibit',
    'access callback' => 'site_configuration_add_image_to_gallery_access',
    //'access arguments' => array('gallery', 2),
    'page callback' => 'site_configuration_add_image_to_gallery',
    'page arguments' => array(2, 3),
  );
  return $items;
}

function site_configuration_add_image_to_gallery_access() {
  return TRUE;
}

function site_configuration_add_image_to_gallery($nid, $fid) {
  global $user;
  $gallery_node = node_load($nid);
  if ($user->uid != $gallery_node->uid) {
    drupal_set_message('Only the exhibit owner can add an image.', 'error');
  }
  $items = $gallery_node->media_gallery_media[LANGUAGE_NONE];
  foreach ($items as $item) {
    $existing_fids[$item['fid']] = TRUE;
  }
  if (empty($existing_fids[$fid])) {
    $file = Array();
    $file['fid'] = $fid;
    $file['title'] = null;
    $file['data'] = '';
    $items[] = $file;
    $gallery_node->media_gallery_media[LANGUAGE_NONE] = $items;
    node_save($gallery_node);
    drupal_set_message(t('The image was added to ' . $gallery_node->title));
  } else {
    drupal_set_message(t('The image already exists in ' . $gallery_node->title . '. No changes happened.'));
  }
  return site_configuration_add_image_to_gallery_form($fid);
}


function site_configuration_add_image_to_gallery_form($fid) {
  global $user;
  $form = array();
  $header = array(
    'exhibit_title' => array('data' => t('Exhibit Title')),//, 'field' => 'email_address'
    'operations' => array('data' => t('Operations')),
  );
  $result =  db_select('node', 'n')
    ->fields('n', array('nid', 'title'))
    ->condition('type', 'media_gallery')
    ->condition('uid', $user->uid)
    ->execute()->fetchAll();
  $rows = array();
  if (count($result) > 0) {
    foreach ($result as $media_gallery) {
      $rows[$media_gallery->nid] = array(
        'exhibit_title' => check_plain($media_gallery->title),
        'operations' => array(
          'data' => array(
            '#type' => 'link',
            '#title' => t('Add to exhibit'),
            '#href' => "exhibit/add/$media_gallery->nid/$fid",
          ),
        ),
      );
    }
  }
  $table = theme('table',
    array(
      'header' => $header,
      'rows' => $rows,
      'empty' => t('No exhibits setup. <a target="_blank" href="' . url('node/add/media-gallery') . '">Add a new exhibit</a> (will open in a new window or tab).'),
      'attributes' => ''
    )
  );
  $img_file = (array) file_load($fid);
  $image = theme('image', array('path' => $img_file['uri']));
  return '<p>' . $image . '</p>' . $table;
}

Comments

lsolesen’s picture

Could you create a patch for others to review which adds this functionality?

ivnish’s picture

Status: Active » Closed (outdated)