protected function ImageFieldFormatter::isValidImage in Entity Embed 8
Checks if the image is valid.
Return value
\Drupal\Core\Access\AccessResult Returns the access result.
1 call to ImageFieldFormatter::isValidImage()
- ImageFieldFormatter::access in src/
Plugin/ entity_embed/ EntityEmbedDisplay/ ImageFieldFormatter.php - Indicates whether this Entity Embed display can be used.
File
- src/
Plugin/ entity_embed/ EntityEmbedDisplay/ ImageFieldFormatter.php, line 115
Class
- ImageFieldFormatter
- Entity Embed Display reusing image field formatters.
Namespace
Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplayCode
protected function isValidImage() {
// If entity type is not file we have to return early to prevent fatal in
// the condition above. Access should already be forbidden at this point,
// which means this won't have any effect.
// @see EntityEmbedDisplayBase::access()
if ($this
->getEntityTypeFromContext() != 'file') {
return AccessResult::forbidden();
}
$access = AccessResult::allowed();
// @todo needs cacheability metadata for getEntityFromContext.
// @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase::getEntityFromContext()
/** @var \Drupal\file\FileInterface $entity */
if ($entity = $this
->getEntityFromContext()) {
// Loading large files is slow, make sure it is an image mime type before
// doing that.
list($type, ) = explode('/', $entity
->getMimeType(), 2);
$is_valid_image = FALSE;
if ($type == 'image') {
$is_valid_image = $this->imageFactory
->get($entity
->getFileUri())
->isValid();
if (!$is_valid_image) {
$this->messenger
->addMessage($this
->t('The selected image "@image" is invalid.', [
'@image' => $entity
->label(),
]), 'error');
}
}
$access = AccessResult::allowedIf($type == 'image' && $is_valid_image)
->addCacheableDependency($entity);
}
return $access;
}