View source
<?php
namespace Drupal\blazy;
class BlazyEntity {
protected $formatter;
public function __construct(BlazyManagerInterface $formatter) {
$this->formatter = $formatter;
}
public function build(array $data, $entity, $fallback = '') {
$build = [];
if (!$entity) {
return [];
}
$settings =& $data['settings'];
if (!empty($data['item'])) {
if (!empty($settings['media_switch'])) {
$is_lightbox = $this->formatter
->getLightboxes() && in_array($settings['media_switch'], $this->formatter
->getLightboxes());
$settings['lightbox'] = $is_lightbox ? $settings['media_switch'] : FALSE;
}
if (empty($settings['uri'])) {
$settings['uri'] = $data['item']->uri;
}
$build = $this->formatter
->getBlazy($data);
$build['#uri'] = empty($settings['uri']) ? '' : $settings['uri'];
if (empty($settings['_detached'])) {
$build['#attached'] = $this->formatter
->attach($settings);
}
}
else {
$build = $this
->entityView($settings['target_type'], $entity, $settings, $fallback);
}
return $build;
}
public function entityView($entity_type, $entity, array $settings, $fallback = '') {
global $language;
$view_hook = $entity_type . '_view';
$view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
$langcode = empty($settings['langcode']) ? $language->language : $settings['langcode'];
$langcode = $langcode !== LANGUAGE_NONE ? $langcode : NULL;
if (function_exists($view_hook)) {
if ($entity_type == 'file') {
$entity->referencing_entity_type = $settings['entity_type_id'];
$entity->referencing_field = $settings['field_name'];
}
return $view_hook($entity, $view_mode, $langcode);
}
elseif (function_exists('entity_view')) {
return entity_view($entity_type, [
$entity,
], $view_mode, $langcode);
}
return $fallback ? [
'#markup' => $fallback,
] : [];
}
public function getFieldValue($entity, $field_name, $settings) {
if ($entity instanceof \Entity) {
$translated = $entity
->wrapper()
->language($settings['langcode'])->{$field_name}
->value();
return $translated ?: $entity
->wrapper()->{$field_name}
->value();
}
return field_get_items($entity->targetType, $entity, $field_name);
}
public function getFieldString($entity, $field_name, $settings, $clean = TRUE) {
if ($value = $this
->getFieldValue($entity, $field_name, $settings)) {
$string = isset($value['value']) ? $value['value'] : $value;
$string = isset($value[0]['value']) ? $value[0]['value'] : $string;
if ($string && is_string($string)) {
$string = $clean ? strip_tags($string, '<a><strong><em><span><small>') : filter_xss($string, BlazyDefault::TAGS);
return trim($string);
}
}
return '';
}
public function getFieldTextOrLink($entity, $field_name, $settings) {
if ($text = $this
->getFieldValue($entity, $field_name, $settings)) {
if (is_array($text)) {
if (isset($text[0]['url']) && !empty($text[0]['title'])) {
$text = $this
->getFieldRenderable($entity, $field_name, $settings, TRUE);
}
elseif ($output = $this
->getFieldString($entity, $field_name, $settings, FALSE)) {
$text = $output;
}
}
return is_string($text) ? [
'#markup' => strip_tags($text, '<a><strong><em><span><small>'),
] : $text;
}
return [];
}
public function getFieldRenderable($entity, $field_name, $settings, $multiple = TRUE) {
if ($field = field_get_items($entity->targetType, $entity, $field_name)) {
$fields = field_view_field($entity->targetType, $entity, $field_name, $settings['view_mode']);
$weight = isset($fields['#weight']) ? $fields['#weight'] : 0;
if ($multiple) {
$items = [];
$entity->_field_view_prepared = FALSE;
foreach (element_children($fields) as $key) {
if (!empty($field[$key]['value']) && isset($field[$key]['format'])) {
$items[] = [
'#markup' => $this
->getFieldString($entity, $field_name, $settings, FALSE),
];
}
else {
$items[] = field_view_value($entity->targetType, $entity, $field_name, $field[$key], $settings['view_mode']);
}
}
$items['#weight'] = $weight;
$entity->_field_view_prepared = TRUE;
return $items;
}
return field_view_value($entity->targetType, $entity, $field_name, $field[0], $settings['view_mode']);
}
return [];
}
}