Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ViewsUiPatternsSourceBase
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 setVariableId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 getPropValue
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 getValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 settingsForm
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getVariableId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_views\Plugin;
6
7use Drupal\Core\Form\FormStateInterface;
8use Drupal\display_builder\RenderableBuilderTrait;
9use Drupal\ui_patterns\PropTypeInterface;
10use Drupal\ui_patterns_views\Plugin\UiPatterns\Source\ViewsSourceBase;
11
12/**
13 * Plugin implementation of the source for views.
14 */
15abstract class ViewsUiPatternsSourceBase extends ViewsSourceBase {
16
17  use RenderableBuilderTrait;
18
19  /**
20   * Set the views variable id passed to views-view.html.twig.
21   *
22   * @return string
23   *   The views variable id.
24   */
25  abstract public static function setVariableId(): string;
26
27  /**
28   * {@inheritdoc}
29   */
30  public function getPropValue(): mixed {
31    $view = $this->getView();
32
33    if (!$view) {
34      return [];
35    }
36
37    $name = self::getVariableId();
38
39    try {
40      $variable = $this->getContextValue('ui_patterns_views:variables')[$name] ?? NULL;
41
42      if ($variable) {
43        return $this->renderOutput($variable);
44      }
45
46      return [];
47    }
48    // @phpcs:ignore SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired
49    catch (\Throwable $th) {
50      // If no context will fail with:
51      // "The ui_patterns_views:variables context is not a valid context."
52      // then we are mostly in preview mode.
53      // @todo have a real preview of the view area content.
54      $build = $this->buildPlaceholder($this->t('[View] @name placeholder', ['@name' => \ucfirst($name)]));
55      $build['#props']['variant'] = 'button';
56
57      return $build;
58    }
59  }
60
61  /**
62   * {@inheritdoc}
63   */
64  public function getValue(?PropTypeInterface $prop_type = NULL): mixed {
65    return $this->getPropValue();
66  }
67
68  /**
69   * {@inheritdoc}
70   */
71  public function settingsForm(array $form, FormStateInterface $form_state): array {
72    $form = parent::settingsForm($form, $form_state);
73    $form['label_map'] = [
74      '#type' => 'html_tag',
75      '#tag' => 'p',
76      '#value' => $this->t('This element must be configured in the corresponding view.'),
77    ];
78
79    return $form;
80  }
81
82  /**
83   * Get the views variable id.
84   *
85   * @return string
86   *   The views variable id.
87   *
88   * phpcs:disable DrupalPractice.Objects.UnusedPrivateMethod.UnusedMethod
89   */
90  private function getVariableId(): string {
91    return $this::setVariableId();
92  }
93
94}