View source
<?php
namespace Drupal\blocktabs\Plugin\Tab;
use Drupal\Core\Form\FormStateInterface;
use Drupal\blocktabs\ConfigurableTabBase;
use Drupal\blocktabs\BlocktabsInterface;
use Drupal\views\Views;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
class ViewsTab extends ConfigurableTabBase {
public function addTab(BlocktabsInterface $blocktabs) {
return TRUE;
}
public function getSummary() {
$markup = $this
->t('view name:') . $this->configuration['view_name'] . '; ';
$markup .= $this
->t('display:') . $this->configuration['view_display'] . '; ';
if (!empty($this->configuration['view_arg'])) {
$markup .= $this
->t('argument:') . $this->configuration['view_arg'];
}
$summary = [
'#markup' => '(' . $markup . ')',
];
return $summary;
}
public function defaultConfiguration() {
return [
'view_name' => NULL,
'view_display' => NULL,
'view_arg' => NULL,
'display_tab' => NULL,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$view_options = Views::getViewsAsOptions(TRUE, 'enabled', NULL, FALSE, TRUE);
$user_input = $form_state
->getUserInput();
$data = isset($user_input['data']) ? $user_input['data'] : [];
$default_view_name = isset($data['view_name']) ? $data['view_name'] : $this->configuration['view_name'];
$form['view_name'] = [
'#type' => 'select',
'#title' => $this
->t('view name'),
'#options' => $view_options,
'#default_value' => $default_view_name,
'#ajax' => [
'callback' => [
$this,
'updateDisplay',
],
'wrapper' => 'edit-view-display-wrapper',
'event' => 'change',
],
'#required' => TRUE,
];
$display_options = [];
if ($default_view_name) {
$view = Views::getView($default_view_name);
foreach ($view->storage
->get('display') as $name => $display) {
$display_options[$name] = $display['display_title'] . ' (' . $display['id'] . ')';
}
}
$form['view_display'] = [
'#type' => 'select',
'#title' => $this
->t('Display'),
'#default_value' => $this->configuration['view_display'],
'#prefix' => '<div id="edit-view-display-wrapper">',
'#suffix' => '</div>',
'#options' => $display_options,
'#required' => TRUE,
];
$form['view_arg'] = [
'#type' => 'textfield',
'#title' => $this
->t('Argument'),
'#default_value' => $this->configuration['view_arg'],
];
$form['display_tab'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display tab even if the view has no result'),
'#default_value' => isset($this->configuration['display_tab']) ? $this->configuration['display_tab'] : 0,
];
return $form;
}
public function updateDisplay(array &$form, FormStateInterface $form_state) {
return $form['data']['view_display'];
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['view_name'] = $form_state
->getValue('view_name');
$this->configuration['view_display'] = $form_state
->getValue('view_display');
$this->configuration['view_arg'] = $form_state
->getValue('view_arg');
$this->configuration['display_tab'] = $form_state
->getValue('display_tab');
}
public function getContent() {
$tab_content = '';
$view_name = $this->configuration['view_name'];
$view_display = $this->configuration['view_display'];
$view_arg = !empty($this->configuration['view_arg']) ? $this->configuration['view_arg'] : NULL;
$view = Views::getView($view_name);
$view
->setDisplay($view_display);
$view
->setArguments($view_arg);
$view
->execute();
$count = count($view->result);
if (!empty($this->configuration['display_tab']) || $count > 0) {
$tab_content = views_embed_view($view_name, $view_display, $view_arg);
}
else {
$tab_content = NULL;
}
return $tab_content;
}
}