prettyphoto_formatters_entityreference.module in prettyPhoto Formatters 7
prettyPhoto Entity reference formatter hooks and callbacks.
File
modules/prettyphoto_formatters_entityreference/prettyphoto_formatters_entityreference.moduleView source
<?php
/**
* @file
* prettyPhoto Entity reference formatter hooks and callbacks.
*/
/**
* Implements hook_field_formatter_info().
*/
function prettyphoto_formatters_entityreference_field_formatter_info() {
$formatters = array();
$formatters = array(
'prettyphoto_entityreference_entity_view' => array(
'label' => t('prettyphoto: Label as link and Rendered entity in Modal'),
'description' => t('Label as link and Rendered entity in Ajax controlled Modal.'),
'field types' => array(
'entityreference',
),
'settings' => array(
'view_mode' => 'default',
),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function prettyphoto_formatters_entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'prettyphoto_entityreference_entity_view') {
$entity_info = entity_get_info($field['settings']['target_type']);
$options = array(
'default' => t('Default'),
);
if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
$options[$view_mode] = $view_mode_settings['label'];
}
}
$element['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('View mode'),
'#default_value' => $settings['view_mode'],
'#access' => count($options) > 1,
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function prettyphoto_formatters_entityreference_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
if ($display['type'] == 'prettyphoto_entityreference_entity_view') {
$entity_info = entity_get_info($field['settings']['target_type']);
$view_mode_label = $settings['view_mode'] == 'default' ? t('Default') : $settings['view_mode'];
if (isset($entity_info['view modes'][$settings['view_mode']]['label'])) {
$view_mode_label = $entity_info['view modes'][$settings['view_mode']]['label'];
}
$summary[] = t('Rendered as @mode', array(
'@mode' => $view_mode_label,
));
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_prepare_view().
*/
function prettyphoto_formatters_entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
$target_ids = array();
// Collect every possible entity attached to any of the entities.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if (isset($item['target_id'])) {
$target_ids[] = $item['target_id'];
}
}
}
if ($target_ids) {
$target_entities = entity_load($field['settings']['target_type'], $target_ids);
}
else {
$target_entities = array();
}
// Iterate through the fieldable entities again to attach the loaded data.
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the referenced entity could be loaded.
if (isset($target_entities[$item['target_id']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
// Check whether the user has access to the referenced entity.
$has_view_access = entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE;
$has_update_access = entity_access('update', $field['settings']['target_type'], $target_entities[$item['target_id']]) !== FALSE;
$items[$id][$delta]['access'] = $has_view_access || $has_update_access;
}
else {
unset($items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey) {
// Rekey the items array.
$items[$id] = array_values($items[$id]);
}
}
}
/**
* Implements hook_field_formatter_view().
*/
function prettyphoto_formatters_entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$result = array();
$settings = $display['settings'];
// Rebuild the items list to contain only those with access.
foreach ($items as $key => $item) {
if (empty($item['access'])) {
unset($items[$key]);
}
}
if ($display['type'] == 'prettyphoto_entityreference_entity_view') {
foreach ($items as $delta => $item) {
$entity = clone $item['entity'];
$result[$delta] = array(
'#theme' => 'prettyphoto_formatters_entityreference',
'#field' => $field,
'#instance' => $instance,
'#entity' => $entity,
'#entity_id' => $item['target_id'],
'#entity_type' => $field['settings']['target_type'],
'#view_mode' => $settings['view_mode'],
);
}
}
return $result;
}
/**
* Implements hook_theme().
*/
function prettyphoto_formatters_entityreference_theme($existing, $type, $theme, $path) {
$theme = array();
$theme['prettyphoto_formatters_entityreference'] = array(
'variables' => array(
'field' => NULL,
'instance' => NULL,
'entity' => NULL,
'entity_id' => NULL,
'entity_type' => NULL,
'view_mode' => NULL,
),
);
foreach ($theme as &$array) {
$array['file'] = 'prettyphoto_formatters_entityreference.formatter.inc';
}
return $theme;
}
/**
* Implements hook_menu().
*/
function prettyphoto_formatters_entityreference_menu() {
$items['prettyphoto/ajax/%/%/%'] = array(
'title' => 'prettyPhoto Ajax Content',
'page callback' => 'prettyphoto_formatters_entityreference_menu_callback',
'page arguments' => array(
2,
3,
4,
),
'access callback' => 'prettyphoto_formatters_entityreference_menu_access_callback',
'access arguments' => array(
2,
3,
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Access callback function for menu callback.
*
* @param string $entity_type
* Entity type e.g: node
* @param int $entity_id
* Entity ID
*
* @return bool
* TRUE if current user has access and FALSE if not.
*/
function prettyphoto_formatters_entityreference_menu_access_callback($entity_type = '', $entity_id = 0) {
if (empty($entity_type) || (int) $entity_id == 0) {
return FALSE;
}
$entities = entity_load($entity_type, array(
$entity_id,
));
foreach ($entities as $entity) {
$has_view_access = entity_access('view', $entity_type, $entity) !== FALSE;
$has_update_access = entity_access('update', $entity_type, $entity) !== FALSE;
if ($has_view_access || $has_update_access) {
return TRUE;
}
}
return FALSE;
}
/**
* Render entity by selected view mode.
*
* @param string $entity_type
* Entity type e.g: node
* @param int $entity_id
* Entity ID
* @param string $view_mode
* Rendered entity's view mode, e.g: default
*/
function prettyphoto_formatters_entityreference_menu_callback($entity_type, $entity_id, $view_mode) {
$result = "";
$entities = entity_load($entity_type, array(
$entity_id,
));
foreach ($entities as $entity) {
unset($entity->content);
$renderable = entity_view($entity_type, array(
$entity,
), $view_mode, NULL, FALSE);
$result .= drupal_render($renderable);
}
print '<div id="pp-loaded-content">' . $result . '</div>';
}
Functions
Name![]() |
Description |
---|---|
prettyphoto_formatters_entityreference_field_formatter_info | Implements hook_field_formatter_info(). |
prettyphoto_formatters_entityreference_field_formatter_prepare_view | Implements hook_field_formatter_prepare_view(). |
prettyphoto_formatters_entityreference_field_formatter_settings_form | Implements hook_field_formatter_settings_form(). |
prettyphoto_formatters_entityreference_field_formatter_settings_summary | Implements hook_field_formatter_settings_summary(). |
prettyphoto_formatters_entityreference_field_formatter_view | Implements hook_field_formatter_view(). |
prettyphoto_formatters_entityreference_menu | Implements hook_menu(). |
prettyphoto_formatters_entityreference_menu_access_callback | Access callback function for menu callback. |
prettyphoto_formatters_entityreference_menu_callback | Render entity by selected view mode. |
prettyphoto_formatters_entityreference_theme | Implements hook_theme(). |