2b79be5ebaa714056b0e41bd1cbc7d0c88e5f162
[yaffs-website] / web / modules / contrib / blazy / src / Dejavu / BlazyEntityTrait.php
1 <?php
2
3 namespace Drupal\blazy\Dejavu;
4
5 use Drupal\Core\Entity\EntityInterface;
6
7 /**
8  * A Trait common for supported entities.
9  *
10  * This file can be imported along with Drupal\blazy\Dejavu\BlazyVideoTrait
11  * to optionally support File/Media Entity where available.
12  */
13 trait BlazyEntityTrait {
14
15   /**
16    * Returns the string value of the fields: link, or text.
17    */
18   public function getFieldString($entity, $field_name = '', $langcode = NULL) {
19     $value = '';
20     $has_field = !empty($field_name) && isset($entity->{$field_name});
21     if (!$has_field) {
22       return $value;
23     }
24
25     if ($entity->hasTranslation($langcode)) {
26       // If the entity has translation, fetch the translated value.
27       $values = $entity->getTranslation($langcode)->get($field_name)->getValue();
28     }
29     else {
30       // Entity doesn't have translation, fetch original value.
31       $values = $entity->get($field_name)->getValue();
32     }
33
34     $value = isset($values[0]['uri']) ? $values[0]['uri'] : (isset($values[0]['value']) ? $values[0]['value'] : '');
35     $value = strip_tags($value);
36
37     return trim($value);
38   }
39
40   /**
41    * Returns the formatted renderable array of the field.
42    */
43   public function getFieldRenderable($entity, $field_name = '', $view_mode = 'full') {
44     $view = [];
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);
48
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';
54     }
55     return $view;
56   }
57
58   /**
59    * Build image/video preview either using theme_blazy(), or view builder.
60    *
61    * This is alternative to Drupal\blazy\BlazyFormatterManager used outside
62    * field formatters, such as Views field, or Entity Browser displays, etc.
63    *
64    * @param array $data
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.
70    *
71    * @return array
72    *   The renderable array of theme_blazy(), or view builder, else empty.
73    */
74   public function buildPreview(array $data, $entity, $fallback = '') {
75     $build = [];
76
77     if (!$entity instanceof EntityInterface) {
78       return [];
79     }
80
81     // Supports VEM/ME if Drupal\blazy\Dejavu\BlazyVideoTrait is imported.
82     if (method_exists($this, 'getMediaItem')) {
83       $this->getMediaItem($data, $entity);
84     }
85
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;
91       }
92       if (empty($settings['uri'])) {
93         $settings['uri'] = ($file = $data['item']->entity) && empty($data['item']->uri) ? $file->getFileUri() : $data['item']->uri;
94       }
95
96       // Provide simple Blazy, if required.
97       if (empty($settings['_basic'])) {
98         $build = $this->blazyManager()->getImage($data);
99       }
100       else {
101         $build = [
102           '#theme'    => 'blazy',
103           '#item'     => $data['item'],
104           '#settings' => $settings,
105         ];
106       }
107
108       // Provides a shortcut to get URI.
109       $build['#uri'] = empty($settings['uri']) ? '' : $settings['uri'];
110
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);
115
116         // Enforces loading elements hidden by EB "Show selected" button.
117         $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
118         $build['#attached'] = $load;
119       }
120     }
121     else {
122       $build = $this->blazyManager()->getEntityView($entity, $settings, $fallback);
123     }
124
125     return $build;
126   }
127
128 }