3 namespace Drupal\blazy\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\ResultRow;
8 use Drupal\blazy\Dejavu\BlazyDefault;
9 use Drupal\blazy\BlazyManagerInterface;
10 use Drupal\blazy\Dejavu\BlazyEntityTrait;
11 use Drupal\blazy\Dejavu\BlazyVideoTrait;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a base views field plugin to render a preview of supported fields.
17 abstract class BlazyViewsFieldPluginBase extends FieldPluginBase {
23 * The blazy service manager.
25 * @var \Drupal\blazy\BlazyManagerInterface
27 protected $blazyManager;
30 * Constructs a BlazyViewsFieldPluginBase object.
32 public function __construct(array $configuration, $plugin_id, $plugin_definition, BlazyManagerInterface $blazy_manager) {
33 parent::__construct($configuration, $plugin_id, $plugin_definition);
34 $this->blazyManager = $blazy_manager;
40 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41 return new static($configuration, $plugin_id, $plugin_definition, $container->get('blazy.manager'));
45 * Returns the blazy admin.
47 public function blazyAdmin() {
48 return \Drupal::service('blazy.admin');
52 * Returns the blazy manager.
54 public function blazyManager() {
55 return $this->blazyManager;
61 protected function defineOptions() {
62 $options = parent::defineOptions();
64 foreach ($this->getDefaultValues() as $key => $default) {
65 $options[$key] = ['default' => $default];
73 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
74 $definitions = $this->getScopedFormElements();
76 $form += $this->blazyAdmin()->baseForm($definitions);
78 foreach ($this->getDefaultValues() as $key => $default) {
79 if (isset($form[$key])) {
80 $form[$key]['#default_value'] = isset($this->options[$key]) ? $this->options[$key] : $default;
81 $form[$key]['#weight'] = 0;
82 if (in_array($key, ['box_style', 'box_media_style'])) {
83 $form[$key]['#empty_option'] = $this->t('- None -');
88 if (isset($form['view_mode'])) {
89 $form['view_mode']['#description'] = $this->t('Will fallback to this view mode, else entity label.');
91 parent::buildOptionsForm($form, $form_state);
97 public function render(ResultRow $values) {
104 public function query() {
105 // Do nothing -- to override the parent query.
109 * Defines the default values.
111 public function getDefaultValues() {
114 'box_media_style' => '',
116 'media_switch' => 'media',
118 'thumbnail_style' => '',
119 'view_mode' => 'default',
124 * Merges the settings.
126 public function mergedViewsSettings() {
129 // Only fetch what we already asked for.
130 foreach ($this->getDefaultValues() as $key => $default) {
131 $settings[$key] = isset($this->options[$key]) ? $this->options[$key] : $default;
134 $settings['count'] = count($this->view->result);
135 $settings['current_view_mode'] = $this->view->current_display;
136 $settings['view_name'] = $this->view->storage->id();
138 return array_merge(BlazyDefault::entitySettings(), $settings);
142 * Defines the scope for the form elements.
144 public function getScopedFormElements() {
146 'settings' => array_filter($this->options),
147 'target_type' => !$this->view->getBaseEntityType() ? '' : $this->view->getBaseEntityType()->id(),
148 'thumbnail_style' => TRUE,