Random Image Block From One or More Galleries Using Views
Now that the Image module hooks into the Views module, we can use both to create a random image block from only selected galleries (the image module provides a random image block, but from all galeries) with no programming necessary.
That said, if you want to skip most of the instructions below, you can use the Views code at the bottom to import this view, but note that you will have to add the filters for the galleries by editing this view once you've imported it.
- install the Image and Views modules. If you already installed Image for 4.7, you may need to re-installl it to get Views functionality.
- enable the "image", "image gallery", "views", and "views ui" modules if you haven't already.
- assign permissions as necessary under administer » access control.
- click administer » image galleries and create your galleries
- upload some images to the gallery through create content » image
- click administer » views » add tab
- give the view a name. In this example, I've used "Random_Image" which differentiates it from the block title the Image module provides for its block. This will be the title of your block in the module configuration page once you've finished.
- optional: assign permissions as to who can view the block. Keeping everything unchecked means everybody can see the block.
- move down to the "Block" heading, click it to show its settings
- check the "Provide Block" box
- in "View Type", select "Table View". This might add a little HTML code that's unnecessary to the block, but it looks a little cleaner when displayed than "List View".
- type in a title, such as "Random Image". This will be the heading of your block that displays on your site once you've enabled it.
- in the "Nodes per Block" setting, type in 1 (the number one).
- optional: add header or footer text.
- there is no page view in this example (though see below), there's no need to click the "[More] Link" setting
- since there will be always one image in the block, the "Empty text" setting won't have any effect
- click the "Fields" heading to see its settings
- under "Add Field", select "Image: Display Image" and click the "Add Field" button
- choose options for the image.
- under "Handler" choose whether it's just the image or a image plus a link to the full node
- under "Option", choose the size of the image. The sizes listed here are determined by the sizes available under administer » settings » image
- skip down to the "Filters" heading and click on it to get its settings
- add filters by dropping down the list under "Add Filter":
- choose "Node: Published"
- choose "Node: Type" then click "Add Filter". Make sure "Is One Of" is selected as the operator, and "image" as the value.
- choose "Taxonomy: Terms for Image Galleries" (this may be called something else if you changed the name of the Image vocabulary in administer » categories) then click "Add Filter". Make sure "Is One Of" is selected. Then choose the galleries you wish to show random images from.
- skip down to "Sort Criteria" and click the heading to get its settings
- under "Add criteria", choose "Random", then click the "Add Criteria" button
- click the "Save" button for the view
- click administer » blocks and enable the new block you created (it will be the same title you gave it in step #12) and place it where you want.
Use the following PHP to import the above view. This only includes up to step #19, sub-step #2. You will have to go in and edit the view to add the galleries to your list of filters.
<?php
$view = new stdClass();
$view->name = 'Random_Image';
$view->description = '';
$view->access = array (
);
$view->view_args_php = '';
$view->block = TRUE;
$view->block_title = 'Random Image';
$view->block_header = '';
$view->block_header_format = '1';
$view->block_footer = '';
$view->block_footer_format = '1';
$view->block_empty = '';
$view->block_empty_format = '1';
$view->block_type = 'table';
$view->nodes_per_block = '1';
$view->block_more = '0';
$view->block_use_page_header = FALSE;
$view->block_use_page_footer = FALSE;
$view->block_use_page_empty = FALSE;
$view->sort = array (
array (
'tablename' => 'node',
'field' => 'random',
'sortorder' => 'ASC',
'options' => '',
),
);
$view->argument = array (
);
$view->field = array (
array (
'tablename' => 'image',
'field' => 'nid',
'label' => '',
'handler' => 'image_views_handler_image_img_link',
'options' => 'thumbnail',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'image',
),
),
);
$view->exposed_filter = array (
);
$view->requires = array(node, image);
$views[$view->name] = $view;
?>