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 $has_field = !empty($field_name) && isset($entity->{$field_name});
25 if ($entity->hasTranslation($langcode)) {
26 // If the entity has translation, fetch the translated value.
27 $values = $entity->getTranslation($langcode)->get($field_name)->getValue();
30 // Entity doesn't have translation, fetch original value.
31 $values = $entity->get($field_name)->getValue();
34 $value = isset($values[0]['uri']) ? $values[0]['uri'] : (isset($values[0]['value']) ? $values[0]['value'] : '');
35 $value = strip_tags($value);
41 * Returns the formatted renderable array of the field.
43 public function getFieldRenderable($entity, $field_name = '', $view_mode = 'full') {
45 $has_field = !empty($field_name) && isset($entity->{$field_name});
46 if ($has_field && !empty($entity->{$field_name}->view($view_mode)[0])) {
47 $view = $entity->get($field_name)->view($view_mode);
49 // Prevents quickedit to operate here as otherwise JS error.
50 // @see 2314185, 2284917, 2160321.
51 // @see quickedit_preprocess_field().
52 // @todo: Remove when it respects plugin annotation.
53 $view['#view_mode'] = '_custom';
59 * Build image/video preview either using theme_blazy(), or view builder.
61 * This is alternative to Drupal\blazy\BlazyFormatterManager used outside
62 * field formatters, such as Views field, or Entity Browser displays, etc.
65 * An array of data containing settings, and image item.
66 * @param object $entity
67 * The media entity, else file entity to be associated to media if any.
68 * @param string $fallback
69 * The fallback string to display such as file name or entity label.
72 * The renderable array of theme_blazy(), or view builder, else empty.
74 public function buildPreview(array $data, $entity, $fallback = '') {
77 if (!$entity instanceof EntityInterface) {
81 // Supports VEM/ME if Drupal\blazy\Dejavu\BlazyVideoTrait is imported.
82 if (method_exists($this, 'getMediaItem')) {
83 $this->getMediaItem($data, $entity);
86 $settings = &$data['settings'];
87 if (!empty($data['item'])) {
88 if (!empty($settings['media_switch'])) {
89 $is_lightbox = $this->blazyManager()->getLightboxes() && in_array($settings['media_switch'], $this->blazyManager()->getLightboxes());
90 $settings['lightbox'] = $is_lightbox ? $settings['media_switch'] : FALSE;
92 if (empty($settings['uri'])) {
93 $settings['uri'] = ($file = $data['item']->entity) && empty($data['item']->uri) ? $file->getFileUri() : $data['item']->uri;
96 // Provide simple Blazy, if required.
97 if (empty($settings['_basic'])) {
98 $build = $this->blazyManager()->getImage($data);
103 '#item' => $data['item'],
104 '#settings' => $settings,
108 // Provides a shortcut to get URI.
109 $build['#uri'] = empty($settings['uri']) ? '' : $settings['uri'];
111 // Allows top level elements to load Blazy once rather than per field.
112 // This is still here for non-supported Views style plugins, etc.
113 if (empty($settings['_detached'])) {
114 $load = $this->blazyManager()->attach($settings);
116 // Enforces loading elements hidden by EB "Show selected" button.
117 $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
118 $build['#attached'] = $load;
122 $build = $this->blazyManager()->getEntityView($entity, $settings, $fallback);