load_block_on_ajax-8.x-1.0/src/Plugin/Condition/LoadBlockOnAjax.php
src/Plugin/Condition/LoadBlockOnAjax.php
<?php namespace Drupal\load_block_on_ajax\Plugin\Condition; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Form\FormStateInterface; /** * Provides a 'Request Path' condition. * * @Condition( * id = "load_block_on_ajax", * label = @Translation("Load Block On Ajax"), * ) */ class LoadBlockOnAjax extends ConditionPluginBase { /** * {@inheritdoc} */ public function defaultConfiguration() { return ['load_block_on_ajax' => ''] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['load_block_on_ajax'] = [ '#type' => 'checkbox', '#title' => $this->t('Load block via AJAX'), '#default_value' => $this->configuration['load_block_on_ajax'], '#description' => $this->t("Check if you want to load block on AJAX."), ]; return parent::buildConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $this->configuration['load_block_on_ajax'] = $form_state->getValue('load_block_on_ajax'); parent::submitConfigurationForm($form, $form_state); $current_path = \Drupal::service('path.current')->getPath(); $arr_path = explode( '/', $current_path ); $var = []; $config = \Drupal::config('load_block_on_ajax.settings'); if( $config->get('load_block_on_ajax_var') != '' ) { $var = unserialize( $config->get('load_block_on_ajax_var') ); } if( TRUE == $form_state->getValue('load_block_on_ajax') ) { $var[$arr_path[5]] = $arr_path[5]; } else { unset( $var[$arr_path[5]] ); } $config = \Drupal::service('config.factory')->getEditable('load_block_on_ajax.settings'); $config->set('load_block_on_ajax_var', serialize($var))->save(); } /** * {@inheritdoc} */ public function summary() { $pages = array_map('trim', explode("\n", $this->configuration['load_block_on_ajax'])); $pages = implode(', ', $pages); if (!empty($this->configuration['negate'])) { return $this->t('Do not return true on the following pages: @pages', ['@pages' => $pages]); } return $this->t('Return true on the following pages: @pages', ['@pages' => $pages]); } /** * {@inheritdoc} */ public function evaluate() { return TRUE; } }