function image_field_caption_entity_storage_load in Image Field Caption 8
Implements hook_entity_storage_load().
File
- ./
image_field_caption.module, line 145 - Provides a caption textarea for image fields.
Code
function image_field_caption_entity_storage_load(array $entities, $entity_type_id) {
$imageCaption = Drupal::service('image_field_caption.storage');
if (in_array($entity_type_id, $imageCaption
->list('entity_type'))) {
// This means we already have some captions.. no need to do all kinds
// of checking then.
/** @var \Drupal\Core\Entity\Entity $entity */
foreach ($entities as $entity) {
// Same load avoiding check.
if (in_array($entity
->bundle(), $imageCaption
->list('bundle'))) {
$needToSave = FALSE;
/** @var \Drupal\Core\Field\FieldItemList $field */
foreach ($entity
->getFields() as $fieldName => $field) {
$values = $entity
->get($fieldName)
->getValue();
foreach ($values as $delta => $value) {
// Get the caption associated to this field.
$revision_id = empty($entity
->getRevisionId()) ? $entity
->id() : $entity
->getRevisionId();
$caption = $imageCaption
->getCaption($entity
->getEntityTypeId(), $entity
->bundle(), $fieldName, $entity
->id(), $revision_id, $entity
->language()
->getId(), $delta);
// Set the caption value.
if (!empty($caption)) {
$values[$delta] = $values[$delta] + $caption;
$needToSave = TRUE;
}
}
if ($needToSave) {
// Save all values.
$entity
->get($fieldName)
->setValue($values);
}
}
}
}
}
}