1

Is there a simple way to add an array of images to a node page?

I would be using the file directory that I am using to upload my images to, using IMCE (sites/theme/files). The array would return all the images I have stored in that folder. I just need a very simple solution, being very new to PHP.

1

Are you using Drupal 6, or Drupal 7? You should look into the Content Construction Kit module, which would easily allow you to add images to nodes and arrange them. You can set the number of allowed elements to "unlimited" for multiple images. If you want to be able to place your node images on different parts of the node page, try the Panels module.

Update in answer to first question, below

Why yes; one of the great strengths of Drupal is its ability to let you just plop down some php anywhere in the page you need to put it (but please, don't put your php in the database, if you can help it!). If all you wanted to do was display images from a folder at a given URL, rendered in your own code, all that you would need would be a menu with 'page callback' set. If you want to modify a node, though, then you will need to learn about the scary render array. When you find something in the page that you want to change, the easiest thing to do is to set the #markup element to the exact HTML content you want to display. If you do not want to replace an element, you can use #prefix or #suffix to put HTML before or after the given item.

If you make yourself a theme, you can add a preprocess hook to it (in template.php):

function MYTHEME_preprocess_node(&$variables, $hook) {
  if ($variables['node']->type == 'page') {
    $variables['content']['field_document_file']['#prefix'] = '<div class="document-wrapper clearfix">';
    $variables['content']['field_document_file']['#suffix'] = '</div>';
  }
}

This snippet, simplified from the above-quoted article on render arrays, adds a div around the CCK field called 'field_document_file'. In your case, you'd probably just want to put some HTML into #suffix after the node body, which I believe is simply $variables['content']['field_document_file']['#suffix']. To find out what's in a render array, install the devel module, and add a dpm($variables) to your preprocess function. You're also likely to find the search krumo module to also be highly useful.

As far as actually getting the images onto the page, I trust that you can write your own code with file_scan_directory. Just remember to write secure code when outputting any data that was supplied by the user.

Anyway, that is what you would do; in general, just adding cck image fields and populating those with the images you currently have stored in folders might be easier. drush entity might help out here. If you have a special need to keep your images in folders, then it's render arrays for you.

An easier option might be to just put your php code in a node-page.tpl.php. I rejected this as an option at first because I felt it would not give you enough flexibility to position your elements in the html output. However, with node-page.tpl.php, you could put your images before or after the node contents, and perhaps adjust the positioning to your satisfaction with css. This would be easier than fixing up the render array.

  • Thanks, Greg. I'm using drupal 7. My aim is to be able to write php code in the node body and display images that are stored in a particular folder. Is there a way to do this? – James30 May 16 '12 at 18:17
  • If you are very new to php, you should go with the CCK solution, which does not require coding. I have adjusted my answer above to describe what you'd have to do to use php on the node, if you want to do that. – greg_1_anderson May 17 '12 at 16:59
  • You might also be interested in: greenash.net.au/thoughts/2012/05/… – greg_1_anderson May 29 '12 at 21:06

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.