3 namespace Drupal\blazy\Dejavu;
5 use Drupal\Core\Entity\EntityInterface;
8 * A Trait common for supported entities.
10 * This file can be imported along with Drupal\blazy\Dejavu\BlazyVideoTrait
11 * to optionally support File/Media Entity where available.
13 trait BlazyEntityTrait {
16 * Returns the string value of the fields: link, or text.
18 public function getFieldString($entity, $field_name = '', $langcode = NULL) {
20 if (empty($field_name)) {
24 if ($entity->hasTranslation($langcode)) {
25 // If the entity has translation, fetch the translated value.
26 $values = $entity->getTranslation($langcode)->get($field_name)->getValue();
29 // Entity doesn't have translation, fetch original value.
30 $values = $entity->get($field_name)->getValue();
33 $value = isset($values[0]['uri']) ? $values[0]['uri'] : (isset($values[0]['value']) ? $values[0]['value'] : '');
34 $value = strip_tags($value);
40 * Returns the formatted renderable array of the field.
42 public function getFieldRenderable($entity, $field_name = '', $view_mode = 'full') {
44 $has_field = !empty($field_name) && isset($entity->{$field_name});
45 if ($has_field && !empty($entity->{$field_name}->view($view_mode)[0])) {
46 $view = $entity->get($field_name)->view($view_mode);
48 // Prevents quickedit to operate here as otherwise JS error.
49 // @see 2314185, 2284917, 2160321.
50 // @see quickedit_preprocess_field().
51 // @todo: Remove when it respects plugin annotation.
52 $view['#view_mode'] = '_custom';
58 * Build image/video preview either using theme_blazy(), or view builder.
60 * This is alternative to Drupal\blazy\BlazyFormatterManager used outside
61 * field formatters, such as Views field, or Entity Browser displays, etc.
64 * An array of data containing settings, and image item.
65 * @param object $entity
66 * The media entity, else file entity to be associated to media if any.
67 * @param string $fallback
68 * The fallback string to display such as file name or entity label.
71 * The renderable array of theme_blazy(), or view builder, else empty.
73 public function buildPreview(array $data, $entity, $fallback = '') {
76 if (!$entity instanceof EntityInterface) {
80 // Supports VEM/ME if Drupal\blazy\Dejavu\BlazyVideoTrait is imported.
81 if (method_exists($this, 'getMediaItem')) {
82 $this->getMediaItem($data, $entity);
85 $settings = &$data['settings'];
86 if (!empty($data['item'])) {
87 if (!empty($settings['media_switch'])) {
88 $is_lightbox = $this->blazyManager()->getLightboxes() && in_array($settings['media_switch'], $this->blazyManager()->getLightboxes());
89 $settings['lightbox'] = $is_lightbox ? $settings['media_switch'] : FALSE;
91 if (empty($settings['uri'])) {
92 $settings['uri'] = ($file = $data['item']->entity) && empty($data['item']->uri) ? $file->getFileUri() : $data['item']->uri;
95 // Provide simple Blazy, if required.
96 if (empty($settings['_basic'])) {
97 $build = $this->blazyManager()->getImage($data);
102 '#item' => $data['item'],
103 '#settings' => $settings,
107 // Provides a shortcut to get URI.
108 $build['#uri'] = empty($settings['uri']) ? '' : $settings['uri'];
110 // Allows top level elements to load Blazy once rather than per field.
111 // This is still here for non-supported Views style plugins, etc.
112 if (empty($settings['_detached'])) {
113 $load = $this->blazyManager()->attach($settings);
115 // Enforces loading elements hidden by EB "Show selected" button.
116 $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
117 $build['#attached'] = $load;
121 $build = $this->blazyManager()->getEntityView($entity, $settings, $fallback);