i just created a new field for flexinode, which might come handy for others as well.

the task:
i needed an additional field in flexinode, which allows to select a album from Gallery2 embeded inside drupal. but not only that, i also wanted to include the generated link into the $links[] (general node-links-bar) of the flexinode page instead of having it listed inside the content.

the way it works:
after adding the field (field_gallery2.inc) to the contrib folder of flexinode and adding the lines in flexinode.module you add the field in your flexinode (content types), which allows you to select one album of your existing albumtree in gallery2. this selection will then be displayed in addition to the general $links[] of flexinode on your flexinode page.
the patch for flexinode.module i use to allow a contrib field to add a 'extralink' to $links[] maybe worth submitting into flexinode.module. this way any contrib field could use this feature.

validation:
the field validates the proper installation of gallery2 and the gallery.module.

requirements:
even though the field requires Gallery2 and gallery.module to be installed to allow the section of a album it's still backward compatible missing one (or both) component and simply acts accordingly (showing now link in flexinode page and displaying helpfull error messages in the create/edit flexinode page.

hope that helps anyone.
cheers

ps: i will be happy to work this into a valide patch-set if it passes the standards of drupal and flexinode.

code patch:
new field field_gallery2.inc:



function flexinode_field_gallery2_name($field) {
  return t('link gallery2');
}

function flexinode_field_gallery2_form($field, &$node) {
  $fieldname = 'flexinode_'. $field->field_id;

/* Only load Gallery Album List if Gallery Module is installed and active */
if (module_exist('gallery')) {

	$options = array(0 => '<'. t('none') .'>');

    list ($success, $ret) = _gallery_init(true);
    if (!$success) {
		return '<strong>'.t($field->label).':</strong><br>'.theme('error', t('Gallery2 needs to be properly installed to use this field. Verify your settings in the gallery module. (Get Gallery2 at http://gallery.menalto.com)'));
    }

	/* We're now recognized as the guest user, we will retrieve all public viewable albums */
	list ($ret, $albumTree) = GalleryCoreApi::fetchAlbumTree();
	if ($ret->isError()) {
		print $ret->getAsHtml();
		return;
	}
	/* You want a flat list of all albumIds, albumTree is a ... tree */
	$albumIds = array_keys_recursive($albumTree);

	/* Load all album objects */
	list ($ret, $albums) = GalleryCoreApi::loadEntitiesById($albumIds);
	if ($ret->isError()) {
		print $ret->getAsHtml();
		return;
	}
	if ($field->required) {
		unset($options[0]);
	}

	$albums = sortAlbums($albums);

	while ((list($key,$album) = each($albums))) {
		$albumTitle = $album->getTitle();
		$albumID = $album->getid();
		/* List of albums in option values */
		$options[$albumID] = $albumTitle;
	}
	return form_select(t($field->label), $fieldname, $node->$fieldname, $options, t($field->description), 0, 0, $field->required);

/* Error message if Gallery Module is not installed or active */
} else {
	return '<strong>'.t($field->label).':</strong><br>'.theme('error', t('The Gallery Module needs to be installed and active to use this field.'));
}
}

function flexinode_field_gallery2_db_select($field) {
  $fieldname = 'flexinode_'. $field->field_id;
  return $fieldname .'.numeric_data AS '. $fieldname;
}

function flexinode_field_gallery2_db_sort_column($field) {
  return 'flexinode_'. $field->field_id .'.numeric_data';
}

function flexinode_field_gallery2_insert($field, $node) {
  $fieldname = 'flexinode_'. $field->field_id;
  db_query('INSERT INTO {flexinode_data} (nid, field_id, numeric_data) VALUES (%d, %d, %d)', $node->nid, $field->field_id, $node->$fieldname);
}

function flexinode_field_gallery2_validate($field, $node) {
  $fieldname = 'flexinode_'. $field->field_id;
}

function flexinode_field_gallery2_format($field, $node, $brief = 0) {
  $fieldname = 'flexinode_'. $field->field_id;
}

function flexinode_field_gallery2_config($field, $edit) {
  //return form_textfield(t('Default value'), 'default_value', $edit['default_value'], 60, 128);
}


/**
 * @addtogroup themeable
 * @{
 */

/**
 * Format a single-line text field for display in a node.
 *
 * @param field_id
 *   Which field is being displayed (useful when overriding this function
 *   if you want to style one particular field differently).
 * @param label
 *   The label for the field as displayed on the node form.
 * @param value
 *   The value that the user entered for the field.
 * @param formatted_value
 *   The value that the user entered for the field as pre-formatted by the module.
 */
function theme_flexinode_gallery2($field_id, $label, $value, $formatted_value) {
  return theme('form_element', $label, $formatted_value);
}

/** @} End of addtogroup themeable */



function flexinode_field_gallery2_extralink($field, $node) {
    $fieldname = 'flexinode_'. $field->field_id;

  $result = db_fetch_object(db_query('SELECT numeric_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id, $node->$fieldname));

	if (module_exist('gallery') && $result->numeric_data > 0) {
    list ($success, $ret) = _gallery_init(true);
    if (!$success) {
      gallery_error(t('Unable to initialize embedded Gallery'), $ret);
      return;
    }
	if (GalleryCoreApi::fetchChildItemIds($result->numeric_data)) {

    $output = '<span class="' . $field . '-link">'. l('<img src="/misc/ic_gallery.gif" width="11" height="11" border="0">' , 'gallery&g2_view=core.ShowItem&g2_itemId='.$result->numeric_data, array("title" => t("View Gallery Album.")), NULL, NULL, FALSE, TRUE) .'</span>';

	}
	}
  return $output;
}



/* Return the array keys of a tree-structured array */
function array_keys_recursive($array) {
	$keys = array();
	foreach ($array as $id => $subArray) {
		$keys = array_merge($keys, array_keys_recursive($subArray));
		$keys[] = $id;
	}
return $keys;
}
 
/* Return the array keys of a tree-structured in sorted order */
function sortAlbums($albumTree){
	$sortedAlbums = array();
	foreach ($albumTree as $sortedAlbum){
		$sortedAlbums[$sortedAlbum->getOriginationTimestamp()] = $sortedAlbum;
	}
	krsort($sortedAlbums);

return $sortedAlbums;
}


patch for flexinode.module starting line 114:


/**
 * Implementation of hook_link().
 */
function flexinode_link($type, $node = 0, $teaser = FALSE) {
  $links = array();

  if ($type == 'node' && strpos($node->type, 'flexinode-') === 0) {
    /* Don't display a redundant edit link if they are node administrators */
    if (flexinode_access('update', $node) && !user_access('administer nodes')) {
      $links[] = l(t('edit this %nodename', array('%nodename' => flexinode_node_name($node))), 'node/'. $node->nid .'/edit');
    }
    }
    /* Get the links from other flexinodes */
  if (!isset($node->ctype_id)) {
    $node->ctype_id = substr($node->type, 10);
  }

  $ctype = flexinode_load_content_type($node->ctype_id);

  foreach ($ctype->fields as $field) {
	if (flexinode_invoke('extralink', $field, $node)) {
		$links[] = flexinode_invoke('extralink', $field, $node);
    }
  }

  return $links;
}

CommentFileSizeAuthor
#2 field_gallery2.inc4.75 KBfuzzydru

Comments

Bèr Kessels’s picture

can you please add this as a file or a patch?

fuzzydru’s picture

StatusFileSize
new4.75 KB

here is the field to download.

to add the patch for flexinode.module insert the following lines into the function flexinode_link on line #126:


<strong>Before:</strong>

/**
 * Implementation of hook_link().
 */
function flexinode_link($type, $node = 0, $teaser = FALSE) {
  $links = array();

  if ($type == 'node' && strpos($node->type, 'flexinode-') === 0) {
    /* Don't display a redundant edit link if they are node administrators */
    if (flexinode_access('update', $node) && !user_access('administer nodes')) {
      $links[] = l(t('edit this %nodename', array('%nodename' => flexinode_node_name($node))), 'node/'. $node->nid .'/edit');
    }
  }

  return $links;
}

 

<strong>After:</strong>

 /**
 * Implementation of hook_link().
 */
function flexinode_link($type, $node = 0, $teaser = FALSE) {
  $links = array();

  if ($type == 'node' && strpos($node->type, 'flexinode-') === 0) {
    /* Don't display a redundant edit link if they are node administrators */
    if (flexinode_access('update', $node) && !user_access('administer nodes')) {
      $links[] = l(t('edit this %nodename', array('%nodename' => flexinode_node_name($node))), 'node/'. $node->nid .'/edit');
    }

    /* Get the links from other flexinodes */
  if (!isset($node->ctype_id)) {
    $node->ctype_id = substr($node->type, 10);
  }

  $ctype = flexinode_load_content_type($node->ctype_id);

  foreach ($ctype->fields as $field) {
	if (flexinode_invoke('extralink', $field, $node)) {
		$links[] = flexinode_invoke('extralink', $field, $node);
    }
  }
    }
  return $links;
}

fuzzydru’s picture

;)
i guess I should mention that the link created by this field is based on a image (icon).

if you want to display a simple text link change the following line #122 in field_gallery2.inc:


<strong>before:</strong>
    $output = '<span class="' . $type . '-link">'. l('<img src="misc/ic_gallery.gif" width="11" height="11" border="0">' , 'gallery&g2_view=core.ShowItem&g2_itemId='.$result->numeric_data, array("title" => t("View Gallery Album.")), NULL, NULL, FALSE, TRUE) .'</span>';

<strong>after:</strong>
    $output = '<span class="' . $type . '-link">'. l(t('See Page Album'), 'gallery&g2_view=core.ShowItem&g2_itemId='.$result->numeric_data, array("title" => t("View Gallery Album.")), NULL, NULL, FALSE, TRUE) .'</span>';

as you also can see the link is 'stylable' via a class. i'm a css-fanatic, so i like to give everything a specific class/id.