MediaImage.php in Varbase Media 9.0.x
File
modules/entity_browser_generic_embed/src/Plugin/entity_embed/EntityEmbedDisplay/MediaImage.php
View source
<?php
namespace Drupal\entity_browser_generic_embed\Plugin\entity_embed\EntityEmbedDisplay;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay\ImageFieldFormatter;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\entity_browser_generic_embed\MediaHelper;
class MediaImage extends ImageFieldFormatter {
public function getFieldFormatterId() {
return 'image';
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
unset($form['image_link']['#options']['content']);
return $form;
}
public function getAttributeValues() {
$field = $this
->getItem();
$label = $field
->getEntity()
->label();
return parent::getAttributeValues() + [
'alt' => $field->alt ?: $label,
'title' => $field->title ?: $label,
];
}
protected function isValidImage() {
return AccessResult::allowed();
}
public function getFieldDefinition() {
return parent::getFieldDefinition()
->setSetting('target_type', 'file');
}
public function getFieldValue() {
$value = parent::getFieldValue();
$value['target_id'] = $this
->getItem()->target_id;
return $value;
}
protected function getItem() {
$entity = $this
->getEntityFromContext();
$item = MediaHelper::getSourceField($entity)
->first();
return $item instanceof ImageItem ? $item : $entity
->get('thumbnail')
->first();
}
}
Classes
Name |
Description |
MediaImage |
Renders a media item's image via the image formatter. |