Skip to content

Commit

Permalink
#132 - Image name sets empty alt tags on media image entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Brant Wynn committed Jul 31, 2019
1 parent a7350cc commit 6fe3b8a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions modules/df/df_tools/df_tools_media/df_tools_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,29 @@ function df_tools_media_media_presave(MediaInterface $entity) {
$unsplash_field = 'field_unsplash_image_id';
$image_field = 'image';
if (in_array($type, $types)
&& $entity->hasField($unsplash_field)
&& !$entity->get($unsplash_field)->isEmpty()
&& $entity->hasField($image_field)
&& $entity->hasField($unsplash_field)
&& !$entity->get($unsplash_field)->isEmpty()
&& $entity->hasField($image_field)
) {
$id = $entity->get($unsplash_field)->value;
$raw_image = file_get_contents('unsplash://' . $id);
$filename = $id . '.jpg';
$file = file_save_data($raw_image, "public://images/$filename", FILE_EXISTS_REPLACE);
$entity->$image_field->setValue(['target_id' => $file->id(), 'alt' => $entity->label()]);
}
elseif (in_array($type, $types)
&& $entity->hasField($image_field)
&& !$entity->get($image_field)->isEmpty()
&& $entity->hasField($unsplash_field)
&& $entity->get($unsplash_field)->isEmpty()
) {
$images_list = $entity->get($image_field)->getValue();
$image = reset($images_list);
if (empty($image['alt'])) {
$image['alt'] = $entity->label();
}
$entity->$image_field->setValue($image);
}
}

/**
Expand Down

0 comments on commit 6fe3b8a

Please sign in to comment.