public function SlickFormatterPlugin::prepareView in Slick Carousel 7.3
Implements hook_field_formatter_prepare_view().
File
- src/
Plugin/ Field/ FieldFormatter/ SlickFormatterPlugin.php, line 94
Class
- SlickFormatterPlugin
- Slick formatter initializer.
Namespace
Drupal\slick\Plugin\Field\FieldFormatterCode
public function prepareView($entity_type, $entities, $field, $instances, &$items, $displays) {
if (!isset($this->isViewPrepared[$field['type']])) {
// Stolen from entityreference which stole from field.api.php to work with
// the supported entity types. We do this to have one base class for
// entities, BlazyEntityBase.
// At D8, this is taken care of by core entity reference, but not at D7.
if (in_array($field['type'], [
'field_collection',
'paragraphs',
])) {
$is_slick = FALSE;
foreach ($displays as $id => $display) {
if ($display['type'] == 'slick' || $display['type'] == 'slick_' . $field['type']) {
$is_slick = TRUE;
break;
}
}
if ($is_slick) {
// @todo avoid hard-coded here, okay for now as we limit field types.
$target_type = $field['type'] . '_item';
$column = 'value';
$target_ids = [];
// Collect every possible entity attached to any of the entities.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if (isset($item[$column])) {
$target_ids[] = $item[$column];
}
}
}
$target_entities = [];
if ($target_ids) {
$target_entities = entity_load($target_type, $target_ids);
}
// Iterate through the fieldable entities again to attach the data.
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the referenced entity could be loaded.
if (is_array($target_entities) && isset($target_entities[$item[$column]]) && isset($target_entities[$item[$column]])) {
// Replace the instance value with the entity data.
$items[$id][$delta]['entity'] = $target_entities[$item[$column]];
// Check whether the user has access to the referenced entity.
$has_view_access = entity_access('view', $target_type, $target_entities[$item[$column]]) !== FALSE;
$has_update_access = entity_access('update', $target_type, $target_entities[$item[$column]]) !== 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]);
}
}
}
}
$this->isViewPrepared[$field['type']] = TRUE;
}
return $this->isViewPrepared[$field['type']];
}