You are here

class CheeseburgerMenuTrigger in Cheeseburger Menu 5.0.x

Provides a 'CheeseburgerMenu' block.

Plugin annotation


@Block(
 id = "cheeseburger_menu_trigger",
 admin_label = @Translation("Cheeseburger menu trigger")
)

Hierarchy

Expanded class hierarchy of CheeseburgerMenuTrigger

File

src/Plugin/Block/CheeseburgerMenuTrigger.php, line 22

Namespace

Drupal\cheeseburger_menu\Plugin\Block
View source
class CheeseburgerMenuTrigger extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Breakpoint manager.
   *
   * @var \Drupal\breakpoint\BreakpointManagerInterface|null
   */
  protected $breakPointManager;

  /**
   * Theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * Cheeseburger menu trigger constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, ThemeManagerInterface $theme_manager, BreakpointManagerInterface $break_point_manager = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->configFactory = $config_factory;
    $this->breakPointManager = $break_point_manager;
    $this->themeManager = $theme_manager;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('config.factory'), $container
      ->get('theme.manager'), $container
      ->has('breakpoint.manager') ? $container
      ->get('breakpoint.manager') : NULL);
  }

  /**
   * {@inheritDoc}
   */
  public function defaultConfiguration() {
    return [
      'block_to_trigger' => NULL,
      'breakpoints' => [],
      'custom_media_query' => NULL,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritDoc}
   */
  public function calculateDependencies() {
    $dependencies = [];
    $block_to_trigger = $this->configuration['block_to_trigger'];
    $cheeseburger_block = $this->entityTypeManager
      ->getStorage('block')
      ->load($block_to_trigger);
    $dependencies['config'][] = $cheeseburger_block
      ->getConfigDependencyName();
    return array_merge($dependencies, parent::calculateDependencies());
  }

  /**
   * {@inheritDoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $conditions = [
      'plugin' => 'cheeseburger_menu',
    ];
    $form_object = $form_state
      ->getFormObject();
    $theme = $this->configFactory
      ->get('system.theme')
      ->get('default');
    if ($form_object && method_exists($form_object, 'getEntity')) {
      $block = $form_object
        ->getEntity();
      $theme = $block
        ->getTheme();
      $conditions['theme'] = $block
        ->getTheme();
    }
    $block_options = array_map(function ($menu_item) {
      return $menu_item
        ->label() . ' (' . $menu_item
        ->id() . ')';
    }, $this->entityTypeManager
      ->getStorage('block')
      ->loadByProperties($conditions));
    $form['block_to_trigger'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Block to trigger'),
      '#options' => $block_options,
      '#default_value' => $this->configuration['block_to_trigger'],
      '#required' => TRUE,
    ];
    $breakpoints = [];
    if ($this->breakPointManager) {
      foreach ($this->breakPointManager
        ->getGroups() as $group_id => $group) {
        $providers = $this->breakPointManager
          ->getGroupProviders($group_id);
        if (isset($providers[$theme])) {
          foreach ($this->breakPointManager
            ->getBreakpointsByGroup($group_id) as $breakpoint) {
            $breakpoints[$breakpoint
              ->getMediaQuery()] = "{$breakpoint->getLabel()} ({$breakpoint->getMediaQuery()})";
          }
        }
      }
    }
    $form['breakpoints'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Breakpoints'),
      '#descriptions' => $this
        ->t('Choose breakpoints on which you want your trigger to appear'),
      '#options' => $breakpoints,
      '#default_value' => $this->configuration['breakpoints'],
      '#access' => FALSE,
    ];
    $form['custom_media_query'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom media query'),
      '#description' => '<ul><li>' . $this
        ->t('Custom media query when trigger will not be displayed. Leave empty to show all the time.') . '</li><li>' . $this
        ->t('Example: @media_query', [
        '@media_query' => 'all and (min-width: 500px)',
      ]) . '</li>',
      '#default_value' => $this->configuration['custom_media_query'],
    ];
    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['block_to_trigger'] = $form_state
      ->getValue('block_to_trigger');
    $this->configuration['breakpoints'] = array_filter($form_state
      ->getValue('breakpoints'));
    $this->configuration['custom_media_query'] = !$form_state
      ->isValueEmpty('custom_media_query') ? $form_state
      ->getValue('custom_media_query') : NULL;
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritDoc}
   */
  public function build() {
    $block_id = 'block-' . str_replace('_', '-', $this->configuration['block_to_trigger']);
    if ($this->themeManager
      ->getActiveTheme()
      ->getName() === 'glisseo') {
      $block_id .= ', .block--' . str_replace('_', '-', $this->configuration['block_to_trigger']);
    }
    return [
      '#theme' => 'cheeseburger_menu_trigger',
      '#menu_id' => $block_id,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::blockForm public function 16
BlockPluginTrait::blockSubmit public function 13
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. Aliased as: traitBuildConfigurationForm
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
CheeseburgerMenuTrigger::$breakPointManager protected property Breakpoint manager.
CheeseburgerMenuTrigger::$configFactory protected property Config factory.
CheeseburgerMenuTrigger::$entityTypeManager protected property Entity type manager service.
CheeseburgerMenuTrigger::$themeManager protected property Theme manager.
CheeseburgerMenuTrigger::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
CheeseburgerMenuTrigger::buildConfigurationForm public function Form constructor. Overrides BlockBase::buildConfigurationForm
CheeseburgerMenuTrigger::calculateDependencies public function Overrides BlockPluginTrait::calculateDependencies
CheeseburgerMenuTrigger::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CheeseburgerMenuTrigger::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
CheeseburgerMenuTrigger::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit(). Overrides BlockPluginTrait::submitConfigurationForm
CheeseburgerMenuTrigger::__construct public function Cheeseburger menu trigger constructor. Overrides BlockPluginTrait::__construct
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginTrait::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginTrait::$initializedContextConfig protected property Tracks whether the context has been initialized from configuration.
ContextAwarePluginTrait::getCacheContexts public function 9
ContextAwarePluginTrait::getCacheMaxAge public function 7
ContextAwarePluginTrait::getCacheTags public function 4
ContextAwarePluginTrait::getContext public function
ContextAwarePluginTrait::getContextDefinition public function
ContextAwarePluginTrait::getContextDefinitions public function
ContextAwarePluginTrait::getContextMapping public function
ContextAwarePluginTrait::getContexts public function
ContextAwarePluginTrait::getContextValue public function
ContextAwarePluginTrait::getContextValues public function
ContextAwarePluginTrait::getPluginDefinition abstract protected function 1
ContextAwarePluginTrait::setContext public function 1
ContextAwarePluginTrait::setContextMapping public function
ContextAwarePluginTrait::setContextValue public function
ContextAwarePluginTrait::validateContexts public function
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
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::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass().
PluginWithFormsTrait::hasFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass().
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.