BlazyStyleBaseTrait.php in Blazy 7
File
src/Dejavu/BlazyStyleBaseTrait.php
View source
<?php
namespace Drupal\blazy\Dejavu;
use Drupal\blazy\Utility\NestedArray;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyDefault;
trait BlazyStyleBaseTrait {
protected $firstImage;
protected $htmlSettings = [];
protected $blazyManager;
public function blazyManager() {
return $this->blazyManager;
}
protected function buildSettings() {
global $language;
$langcode = isset($language->language) ? $language->language : LANGUAGE_NONE;
$view = $this->view;
$count = count($view->result);
$settings = $this->options;
$view_name = $view->name;
$view_mode = $view->current_display;
$instance = str_replace('_', '-', "{$view_name}-{$view_mode}");
$plugin_id = $view->plugin_name;
$id = empty($settings['id']) ? '' : $settings['id'];
$id = Blazy::getHtmlId("{$plugin_id}-views-{$instance}", $id);
$settings += [
'cache_metadata' => [
'keys' => [
$id,
$view_mode,
$count,
$langcode,
],
],
] + BlazyDefault::lazySettings();
$settings['check_blazy'] = TRUE;
$settings['id'] = $id;
$settings['count'] = $count;
$settings['current_view_mode'] = $view_mode;
$settings['instance_id'] = $instance;
$settings['multiple'] = TRUE;
$settings['plugin_id'] = $plugin_id;
$settings['use_ajax'] = $view->use_ajax;
$settings['view_name'] = $view_name;
$settings['view_display'] = $view->style_plugin->display->display_plugin;
$settings['_views'] = TRUE;
if (!empty($this->htmlSettings)) {
$settings = NestedArray::mergeDeep($settings, $this->htmlSettings);
}
drupal_alter('blazy_settings_views', $settings, $view);
return $settings;
}
protected function setHtmlSettings(array $settings = []) {
$this->htmlSettings = $settings;
return $this;
}
public function getFirstImage($row) {
if (!isset($this->firstImage)) {
$rendered = [];
if ($row) {
foreach ($row as $item) {
if (is_array($item) && isset($item[0]['rendered']) && isset($item[0]['rendered']['#build'])) {
$rendered = $item[0]['rendered']['#build'];
break;
}
}
}
$this->firstImage = $rendered;
}
return $this->firstImage;
}
public function getFieldRenderable($row, $index, $field_name = '', $multiple = FALSE) {
$field = $this->view->field[$field_name]->handler_type . '_' . $field_name;
return $multiple && isset($row->{$field}) ? $row->{$field} : (isset($row->{$field}[0]) ? $row->{$field}[0] : '');
}
}