iss.module in Image Style Selector 7
Same filename and directory in other branches
Image Style Selector field.
The Image Style Selector (ISS) field allows users to select the image style that should be applied on the image. The instance settings of the ISS field allows to specify which image styles are available for the user and to which image field the image style should be applied.
File
iss.moduleView source
<?php
/**
* @file
* Image Style Selector field.
*
* The Image Style Selector (ISS) field allows users to select the image style
* that should be applied on the image. The instance settings of the ISS field
* allows to specify which image styles are available for the user and to which
* image field the image style should be applied.
*/
/**
* Implements hook_help().
*/
function iss_help($path, $arg) {
switch ($path) {
case 'admin/help#iss':
// Construct the path of this module's readme file.
$path_readme = drupal_get_path('module', 'iss') . '/README.txt';
// If the readme is available, return the contents.
if (file_exists($path_readme)) {
$readme = file_get_contents($path_readme);
return '<h1>README.txt</h1><pre>' . check_plain($readme) . '</pre>';
}
}
}
/**
* Implements hook_field_info().
*/
function iss_field_info() {
return array(
'iss' => array(
'label' => t('Image Style Selector'),
'description' => t('This field allows users to select the image style that should be applied to an image.'),
'instance_settings' => array(
'image_field_name' => NULL,
'image_styles' => array(),
),
'default_widget' => 'iss_widget_radios',
'default_formatter' => 'iss_formatter',
'property_type' => 'text',
),
);
}
/**
* Implements hook_field_is_empty().
*/
function iss_field_is_empty($item, $field) {
if ($field['type'] == 'iss') {
return empty($item['image_style']);
}
return FALSE;
}
/**
* Implements hook_field_instance_settings_form().
*/
function iss_field_instance_settings_form($field, $instance) {
$form = array();
if ($field['type'] == 'iss') {
$form['image_field_name'] = array(
'#type' => 'select',
'#title' => t('Image field'),
'#description' => t('Select the image field where the image style has to be applied to.'),
'#options' => iss_image_field_name_options($instance['entity_type'], $instance['bundle']),
'#default_value' => $instance['settings']['image_field_name'],
);
$form['image_styles'] = array(
'#type' => 'checkboxes',
'#title' => t('Image styles'),
'#description' => t('Select the image styles that should be available for the user to select.'),
'#options' => image_style_options(FALSE, PASS_THROUGH),
'#default_value' => $instance['settings']['image_styles'],
'#required' => TRUE,
'#element_validate' => array(
'iss_image_styles_options_validate',
),
);
}
return $form;
}
/**
* Retrieves a list of image fields that are available for the entity's bundle.
*
* @param string $entity_type
* The machine name of the entity type.
* @param string $bundle
* The machine name of the bundle.
*
* @return string[]
* Returns machine names of all image fields that are available for the
* provided entity's bundle.
*/
function iss_image_field_name_options($entity_type, $bundle) {
$image_field_names = array();
// Get all fields that are associated to this entity's bundle.
$fields = field_info_instances($entity_type, $bundle);
foreach ($fields as $field_name => $value) {
$field = field_info_field($field_name);
// When it is an image field add it to the array that will be returned.
if ($field['type'] == 'image') {
$image_field_names[$field_name] = $fields[$field_name]['label'];
}
}
return $image_field_names;
}
/**
* Removes all not selected options form the result.
*/
function iss_image_styles_options_validate($element, &$form_state) {
form_set_value($element, $element['#value'], $form_state);
}
/**
* Implements hook_field_widget_info().
*/
function iss_field_widget_info() {
return array(
'iss_widget_radios' => array(
'label' => t('Radio buttons'),
'field types' => array(
'iss',
),
'behaviors' => array(
// @todo what is the desired functionality when multiple values are
// allowed.
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
'iss_widget_select' => array(
'label' => t('Select list'),
'field types' => array(
'iss',
),
'behaviors' => array(
// @todo what is the desired functionality when multiple values are
// allowed.
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function iss_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$image_styles = array();
// Show the label to the user (instead of the machine name).
// NB: this also removes styles that aren't valid selections anymore (e.g.,
// image style that is deleted).
foreach ($instance['settings']['image_styles'] as $machine_name) {
$image_style = image_style_load($machine_name);
$image_styles[$machine_name] = $image_style['label'];
}
// Set general properties that all widgets have in common.
$element['image_style'] = array(
'#title' => t('Image style'),
);
switch ($instance['widget']['type']) {
case 'iss_widget_radios':
$element['image_style'] += array(
'#type' => 'radios',
'#options' => $image_styles,
'#default_value' => isset($items[$delta]['image_style']) ? $items[$delta]['image_style'] : NULL,
);
break;
case 'iss_widget_select':
$element['image_style'] += array(
'#type' => 'select',
'#options' => $image_styles,
'#default_value' => isset($items[$delta]['image_style']) ? $items[$delta]['image_style'] : NULL,
'#multiple' => FALSE,
);
break;
}
return $element;
}
/**
* Implements hook_field_formatter_info().
*/
function iss_field_formatter_info() {
return array(
'iss_formatter' => array(
'label' => t('Selected Image Style'),
'field types' => array(
'iss',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function iss_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($display['type'] == 'iss_formatter') {
foreach ($items as $delta => $item) {
$images = field_get_items($entity_type, $entity, $instance['settings']['image_field_name'], $langcode);
if (!empty($images)) {
foreach ($images as $image_delta => $image) {
$element[$delta][$image_delta] = array(
'#theme' => 'image_formatter',
'#item' => $image,
'#image_style' => $item['image_style'],
);
}
}
}
}
return $element;
}
Functions
Name | Description |
---|---|
iss_field_formatter_info | Implements hook_field_formatter_info(). |
iss_field_formatter_view | Implements hook_field_formatter_view(). |
iss_field_info | Implements hook_field_info(). |
iss_field_instance_settings_form | Implements hook_field_instance_settings_form(). |
iss_field_is_empty | Implements hook_field_is_empty(). |
iss_field_widget_form | Implements hook_field_widget_form(). |
iss_field_widget_info | Implements hook_field_widget_info(). |
iss_help | Implements hook_help(). |
iss_image_field_name_options | Retrieves a list of image fields that are available for the entity's bundle. |
iss_image_styles_options_validate | Removes all not selected options form the result. |