You are here

class UltimenuBlock in Ultimenu 8.2

Same name in this branch
  1. 8.2 src/Plugin/Derivative/UltimenuBlock.php \Drupal\ultimenu\Plugin\Derivative\UltimenuBlock
  2. 8.2 src/Plugin/Block/UltimenuBlock.php \Drupal\ultimenu\Plugin\Block\UltimenuBlock
Same name and namespace in other branches
  1. 8 src/Plugin/Block/UltimenuBlock.php \Drupal\ultimenu\Plugin\Block\UltimenuBlock

Provides an 'Ultimenu' block.

Plugin annotation


@Block(
 id = "ultimenu_block",
 admin_label = @Translation("Ultimenu block"),
 category = @Translation("Ultimenu"),
 deriver = "Drupal\ultimenu\Plugin\Derivative\UltimenuBlock",
)

Hierarchy

Expanded class hierarchy of UltimenuBlock

File

src/Plugin/Block/UltimenuBlock.php, line 25

Namespace

Drupal\ultimenu\Plugin\Block
View source
class UltimenuBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * The Ultimenu manager service.
   *
   * @var \Drupal\ultimenu\UltimenuManagerInterface
   */
  protected $ultimenuManager;

  /**
   * The Ultimenu skin service.
   *
   * @var \Drupal\ultimenu\UltimenuSkinInterface
   */
  protected $ultimenuSkin;

  /**
   * Constructs an UltimenuBlock object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The ultimenu manager.
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The ultimenu manager.
   * @param \Drupal\ultimenu\UltimenuManagerInterface $ultimenu_manager
   *   The ultimenu manager.
   * @param \Drupal\ultimenu\UltimenuSkinInterface $ultimenu_skin
   *   The ultimenu skin service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $current_user, LibraryDiscoveryInterface $library_discovery, UltimenuManagerInterface $ultimenu_manager, UltimenuSkinInterface $ultimenu_skin) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->currentUser = $current_user;
    $this->libraryDiscovery = $library_discovery;
    $this->ultimenuManager = $ultimenu_manager;
    $this->ultimenuSkin = $ultimenu_skin;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('current_user'), $container
      ->get('library.discovery'), $container
      ->get('ultimenu.manager'), $container
      ->get('ultimenu.skin'));
  }

  /**
   * {@inheritdoc}
   */
  public function delta() {

    // Derivatives are prefixed with 'ultimenu-', e.g.: ultimenu-main.
    $id = $this
      ->getDerivativeId();
    return substr($id, 9);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'ajaxify' => FALSE,
      'regions' => [],
      'skin' => 'module|ultimenu--dark',
      'submenu' => FALSE,
      'orientation' => 'ultimenu--htb',
      'submenu_position' => '',
      'canvas_off' => '#header',
      'canvas_on' => '#main-wrapper, .featured-top, .site-footer',
      'canvas_skin' => 'scalein',
    ];
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockForm().
   */
  public function blockForm($form, FormStateInterface $form_state) {
    if ($this->currentUser
      ->hasPermission('administer ultimenu')) {
      $ultimenu_admin = Url::fromRoute('ultimenu.settings')
        ->toString();
      $form['ajaxify'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Ajaxify'),
        '#default_value' => isset($this->configuration['ajaxify']) ? $this->configuration['ajaxify'] : FALSE,
        '#description' => $this
          ->t('Check to load ultimenu region contents using AJAX. Only makes sense for massive contents.'),
      ];

      // @todo all: $regions = (array) $this->ultimenuManager->getSetting('regions');
      $regions = $this->ultimenuManager
        ->getRegionsByMenu($this
        ->delta());
      $states['visible'][':input[name="settings[ajaxify]"]'] = [
        'checked' => TRUE,
      ];
      $form['regions'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Ajaxifed regions'),
        '#options' => $regions,
        '#default_value' => isset($this->configuration['regions']) ? array_values((array) $this->configuration['regions']) : [],
        '#description' => $this
          ->t('Check which regions should be ajaxified, leaving those unchecked as non-ajaxed regions. Be sure to enable the regions at <a href=":url">Ultimenu admin</a>.', [
          ':url' => $ultimenu_admin,
        ]),
        '#states' => $states,
      ];
      $form['skin'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Ultimenu skin'),
        '#default_value' => $this->configuration['skin'],
        '#options' => $this->ultimenuSkin
          ->loadMultiple(),
        '#empty_option' => $this
          ->t('- None -'),
        '#description' => $this
          ->t('Choose the skin for this block. You can supply custom skins at <a href=":ultimenu_settings" target="_blank">Ultimenu settings</a>. The skin can be made specific to this block using the proper class by each menu name. Be sure to <a href=":clear" target="_blank">clear the cache</a> if trouble to see the new skin applied.', [
          ':ultimenu_settings' => $ultimenu_admin,
          ':clear' => Url::fromRoute('system.performance_settings')
            ->toString(),
        ]),
      ];
      $form['orientation'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Flyout orientation'),
        '#default_value' => $this->configuration['orientation'],
        '#options' => [
          'ultimenu--htb' => $this
            ->t('Horizontal to bottom'),
          'ultimenu--htt' => $this
            ->t('Horizontal to top'),
          'ultimenu--vtl' => $this
            ->t('Vertical to left'),
          'ultimenu--vtr' => $this
            ->t('Vertical to right'),
        ],
        '#description' => $this
          ->t('Choose the orientation of the flyout, depending on the placement. At sidebar left, <strong>Vertical to right</strong>. At header, <strong>Horizontal to bottom</strong>. At footer, <strong>Horizontal to top</strong>'),
      ];
      $form['submenu'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Render submenu'),
        '#default_value' => $this->configuration['submenu'],
        '#description' => $this
          ->t('Render the relevant submenus inside the Ultimenu region without using Block admin, and independent from blocks. Alternatively use core Menu level option with regular menu block when core supports the "Fixed parent item", see <a href=":url" target="_blank">#2631468</a>. <br /><strong>Important!</strong> Be sure to check "<strong>Show as expanded</strong>" at the parent menu item edit page as needed, otherwise no submenus will be rendered.', [
          ':url' => 'https://www.drupal.org/node/2631468',
        ]),
      ];
      $form['submenu_position'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Submenu position'),
        '#options' => [
          'bottom' => $this
            ->t('Bottom'),
          'top' => $this
            ->t('Top'),
        ],
        '#empty_option' => $this
          ->t('- None -'),
        '#default_value' => $this->configuration['submenu_position'],
        '#description' => $this
          ->t('Choose where to place the submenu, either before or after existing blocks. Default to Top.'),
      ];
      if ($this->configuration['id'] == 'ultimenu_block:ultimenu-main') {
        $form['canvas_off'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Off-canvas element'),
          '#default_value' => $this->configuration['canvas_off'],
          '#description' => $this
            ->t('Valid CSS selector for the off-canvas element. Only one can exist, for Bartik, e.g.: <code>#header</code> or <code>.region-primary-menu</code> (not good, just works). But not both.'),
        ];
        $form['canvas_on'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('On-canvas element'),
          '#default_value' => $this->configuration['canvas_on'],
          '#description' => $this
            ->t('Valid CSS selector for the on-canvas element. Can be multiple, for Bartik, e.g.: <code>#main-wrapper, .highlighted, .featured-top, .site-footer</code> <br>Visit <b>/admin/help/ultimenu</b> under <b>STYLING</b> section for details.'),
        ];
        $skins = $this->ultimenuSkin
          ->getOffCanvasSkins();
        $form['canvas_skin'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Off-canvas skin'),
          '#options' => array_combine($skins, $skins),
          '#default_value' => $this->configuration['canvas_skin'],
          '#description' => $this
            ->t('The off-canvas skin. Note the name oldies is meant for old browsers up, but not as smoother. Consider Modernizr.js to support old browsers with advanced transform effects. More custom works are required as usual.'),
        ];
      }
    }
    return $form;
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockSubmit().
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    foreach (array_keys($this
      ->defaultConfiguration()) as $key) {
      $value = $form_state
        ->getValue($key);
      $this->configuration[$key] = $key == 'regions' ? array_filter($value) : $value;
    }

    // Invalidate the library discovery cache to update the new skin discovery.
    $this->ultimenuSkin
      ->clearCachedDefinitions();
    $this->libraryDiscovery
      ->clearCachedDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $menu_name = $this
      ->delta();
    $skin = $this->configuration['skin'];
    $provider = '';
    $skin_name = '';
    $skin_basename = '';

    // Load the specified block skin.
    if (!empty($skin)) {
      $skin_css_path = $this->ultimenuSkin
        ->getPath($skin);
      $skin_basename = $this->ultimenuSkin
        ->getName($skin_css_path);

      // Fetch the skin file name from the setting.
      list($provider, $skin_name) = array_pad(array_map('trim', explode("|", $skin, 2)), 2, NULL);
    }

    // Provide the settings for further process.
    $build['config'] = [
      'bid' => $this
        ->getDerivativeId(),
      'menu_name' => $menu_name,
      'regions' => empty($this->configuration['regions']) ? [] : array_filter($this->configuration['regions']),
      'skin_name' => $skin_name,
      'skin_provider' => $provider,
      'skin_basename' => $skin_basename,
    ] + $this->configuration;
    return $this->ultimenuManager
      ->build($build);
  }

}

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::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. 2
BlockPluginTrait::calculateDependencies public function
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::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
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
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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::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.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
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.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2
UltimenuBlock::$currentUser protected property The current user.
UltimenuBlock::$libraryDiscovery protected property The library discovery service.
UltimenuBlock::$ultimenuManager protected property The Ultimenu manager service.
UltimenuBlock::$ultimenuSkin protected property The Ultimenu skin service.
UltimenuBlock::blockForm public function Overrides \Drupal\block\BlockBase::blockForm(). Overrides BlockPluginTrait::blockForm
UltimenuBlock::blockSubmit public function Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockPluginTrait::blockSubmit
UltimenuBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
UltimenuBlock::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
UltimenuBlock::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
UltimenuBlock::delta public function
UltimenuBlock::__construct public function Constructs an UltimenuBlock object. Overrides BlockPluginTrait::__construct