You are here

class ViewEmbed in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Dashboard/ViewEmbed.php \Drupal\dashboards\Plugin\Dashboard\ViewEmbed

Show account info.

Plugin annotation


@Dashboard(
  id = "view_embed",
  label = @Translation("Embed a view"),
  category = @Translation("Views")
)

Hierarchy

Expanded class hierarchy of ViewEmbed

File

src/Plugin/Dashboard/ViewEmbed.php, line 23

Namespace

Drupal\dashboards\Plugin\Dashboard
View source
class ViewEmbed extends DashboardBase {

  /**
   * Entity query.
   *
   * @var \Drupal\Core\Entity\Query\QueryInterface
   */
  protected $entityQuery;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache_backend, QueryInterface $query) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $cache_backend);
    $this->entityQuery = $query;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('dashboards.cache'), $container
      ->get('entity_type.manager')
      ->getStorage('view')
      ->getQuery());
  }

  /**
   * {@inheritdoc}
   */
  public function buildRenderArray($configuration) : array {
    $viewId = explode(':', $configuration['view']);
    $view = Views::getView($viewId[0]);
    if (!$view) {
      return [
        '#markup' => 'View do not exist anymore',
      ];
    }
    return views_embed_view($viewId[0], $viewId[1]);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array $form, FormStateInterface $form_state, array $configuration) : void {
    if (!$form_state
      ->getValue('view')) {
      $form_state
        ->setErrorByName('view', $this
        ->t('Please provide a view.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
    $views = $this->entityQuery
      ->condition('status', TRUE)
      ->condition("display.*.display_plugin", [
      'embed',
    ], 'IN')
      ->execute();
    $views = View::loadMultiple($views);
    $options = [];
    foreach ($views as $view) {
      foreach ($view
        ->get('display') as $display) {
        if ($display['display_plugin'] != 'embed') {
          continue;
        }
        $options[$view
          ->id() . ':' . $display['id']] = $view
          ->label() . ': ' . $display['display_title'];
      }
    }
    if (empty($options)) {
      $form['view'] = [
        '#markup' => $this
          ->t('Please add a embed view to use this plugin.'),
      ];
      return $form;
    }
    $form['view'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#options' => $options,
      '#default_value' => isset($configuration['view']) ? $configuration['view'] : FALSE,
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DashboardBase::$cache protected property Cache backend.
DashboardBase::getCache protected function Get cache for cid.
DashboardBase::massageFormValues public function Validate settings form.
DashboardBase::setCache protected function Set a new cache entry. Cache is prefixed by pluginid.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ViewEmbed::$entityQuery protected property Entity query.
ViewEmbed::buildRenderArray public function Build render array. Overrides DashboardBase::buildRenderArray
ViewEmbed::buildSettingsForm public function Build render array. Overrides DashboardBase::buildSettingsForm
ViewEmbed::create public static function Creates an instance of the plugin. Overrides DashboardBase::create
ViewEmbed::validateForm public function Validate settings form. Overrides DashboardBase::validateForm
ViewEmbed::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DashboardBase::__construct