View source
<?php
namespace Drupal\insert_view_adv\Plugin\Filter;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\views\Views;
class InsertView extends FilterBase implements TrustedCallbackInterface {
public function process($text, $langcode) {
$matches = [];
$encoded_configuration = Json::encode($this
->getConfiguration());
$result = new FilterProcessResult($text);
$count = preg_match_all("/\\[view:([^=\\]]+)=?([^=\\]]+)?=?([^\\]]*)?\\]/i", $text, $matches);
$insert_view_count = 0;
if ($count) {
$search = $replace = [];
foreach ($matches[0] as $key => $value) {
$view_name = $matches[1][$key];
$display_id = $matches[2][$key] && !is_numeric($matches[2][$key]) ? $matches[2][$key] : 'default';
$args = $matches[3][$key];
if (!empty($this->settings['hide_argument_input'])) {
$args = '';
}
$view_output = $result
->createPlaceholder('\\Drupal\\insert_view_adv\\Plugin\\Filter\\InsertView::build', [
$view_name,
$display_id,
$args,
$encoded_configuration,
]);
$search[] = $value;
$replace[] = $view_output;
}
$text = str_replace($search, $replace, $text, $insert_view_count);
}
$count = preg_match_all('/(<p>)?(?<json>{(?=.*inserted_view_adv\\b)(?=.*arguments\\b)(.*)})(<\\/p>)?/', $text, $matches);
if ($count) {
$search = $replace = [];
foreach ($matches['json'] as $key => $value) {
$inserted = Json::decode($value);
if (!is_array($inserted) || empty($inserted)) {
continue;
}
$view_parts = explode('=', $inserted['inserted_view_adv']);
if (empty($view_parts)) {
continue;
}
$view_name = $view_parts[0];
$display_id = $view_parts[1] && !is_numeric($view_parts[1]) ? $view_parts[1] : 'default';
$args = '';
if (!empty($inserted['arguments']) && empty($this->settings['hide_argument_input'])) {
$args = implode('/', $inserted['arguments']);
}
$view_output = $result
->createPlaceholder('\\Drupal\\insert_view_adv\\Plugin\\Filter\\InsertView::build', [
$view_name,
$display_id,
$args,
$encoded_configuration,
]);
$search[] = $value;
$replace[] = $view_output;
}
$text = str_replace($search, $replace, $text, $insert_view_count);
}
if ($insert_view_count > 0) {
$result
->setProcessedText($text)
->addCacheTags([
'insert_view_adv',
])
->addCacheContexts([
'url',
'user.permissions',
]);
}
return $result;
}
public static function build($view_name, $display_id, $args, $configuration) {
$plain = '';
if (!is_array($configuration)) {
$configuration = Json::decode($configuration);
}
if ($configuration && isset($configuration['settings']['render_as_empty']) && $configuration['settings']['render_as_empty'] == 0) {
$plain = '[view:' . $view_name . '=' . $display_id;
if (!empty($args)) {
$plain .= '=' . $args;
}
$plain .= ']';
}
if (empty($view_name)) {
return [
'#attached' => [],
'#markup' => $plain,
];
}
if ($configuration && !empty($configuration['settings']['allowed_views'])) {
$allowed_views = array_filter($configuration['settings']['allowed_views']);
if (!empty($allowed_views) && empty($allowed_views[$view_name . '=' . $display_id])) {
return [
'#attached' => [],
'#markup' => $plain,
];
}
}
$view = Views::getView($view_name);
if (empty($view)) {
return [
'#attached' => [],
'#markup' => $plain,
];
}
if (!$view
->access($display_id)) {
return [
'#attached' => [],
'#markup' => $plain,
];
}
$view
->setDisplay($display_id);
$request = \Drupal::service('request_stack')
->getCurrentRequest();
$current_path = $request
->getPathInfo();
if (\Drupal::currentUser()
->isAuthenticated() && \Drupal::moduleHandler()
->moduleExists('big_pipe')) {
$op = $request
->get('op');
$display_options = $view->display_handler
->getOption('exposed_form');
if (!is_null($op) && !empty($display_options) && $op == $display_options['options']['reset_button_label']) {
return [
'#attached' => [
'drupalSettings' => [
'insert_view_adv' => [
'reset_redirect' => $current_path,
],
],
'library' => [
'insert_view_adv/reset_redirect',
],
],
];
}
}
$url_args = explode('/', $current_path);
foreach ($url_args as $id => $arg) {
$args = str_replace("%{$id}", $arg, $args);
}
$args = preg_replace(',/?(%\\d),', '', $args);
$args = $args ? explode('/', $args) : [];
return $view
->preview($display_id, $args) ?: [];
}
public function tips($long = FALSE) {
if ($long) {
$examples = [
'[view:my_view]',
'[view:my_view=my_display]',
'[view:my_view=my_display=arg1/arg2/arg3]',
'[view:my_view==arg1/arg2/arg3]',
];
$items = [
$this
->t('Insert view filter allows to embed views using tags. The tag syntax is relatively simple: [view:name=display=args]'),
$this
->t('For example [view:tracker=page=1] says, embed a view named "tracker", use the "page" display, and supply the argument "1".'),
$this
->t('The <em>display</em> and <em>args</em> parameters can be omitted. If the display is left empty, the view\'s default display is used.'),
$this
->t('Multiple arguments are separated with slash. The <em>args</em> format is the same as used in the URL (or view preview screen).'),
[
'data' => $this
->t('Valid examples'),
'children' => $examples,
],
];
$list = [
'#type' => 'item_list',
'#items' => $items,
];
return render($list);
}
else {
return $this
->t('You may use [view:<em>name=display=args</em>] tags to display views.');
}
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$views_list = Views::getEnabledViews();
$options = [];
foreach ($views_list as $machine_name => $view) {
foreach ($view
->get('display') as $display) {
$display_title = !empty($display['display_options']['title']) ? $display['display_options']['title'] : $display['display_title'];
$options[$machine_name . '=' . $display['id']] = $this
->t('@view_name: @display_title (@display_name)', [
'@view_name' => $view
->label(),
'@display_title' => $display_title,
'@display_name' => $display['id'],
]);
}
}
$form['allowed_views'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed views to insert'),
'#description' => $this
->t('Leave empty to allow all views'),
'#options' => $options,
'#default_value' => $this->settings['allowed_views'],
];
$form['render_as_empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Do not render disabled/not allowed views'),
'#default_value' => $this->settings['render_as_empty'],
'#description' => $this
->t('If unchecked the disabled/not allowed view will be rendered as token [view:view_name=display_id=args]'),
];
$form['hide_argument_input'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide views arguments (contextual filters) input'),
'#default_value' => $this->settings['hide_argument_input'],
'#description' => $this
->t('If checked the user will not be allowed to input the argument values, only default will be used.'),
];
return $form;
}
public function setConfiguration(array $configuration) {
if (!empty($configuration['settings']['allowed_views'])) {
$configuration['settings']['allowed_views'] = array_filter($configuration['settings']['allowed_views']);
}
parent::setConfiguration($configuration);
}
public static function trustedCallbacks() {
return [
'build',
];
}
}