View source
<?php
namespace Drupal\domain_menus\Plugin\Block;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\system\Entity\Menu;
use Drupal\system\Plugin\Block\SystemMenuBlock;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DomainMenusMenuBlock extends SystemMenuBlock {
protected $configFactory;
protected $domainNegotiator;
const SETTINGS = 'domain_menus.settings';
public $settings = [];
public function __construct($configuration, $plugin_id, $plugin_definition, $menu_tree, $menu_active_trail, ConfigFactoryInterface $config_factory, DomainNegotiatorInterface $domain_negotiator) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $menu_tree, $menu_active_trail);
$this->configFactory = $config_factory;
$this->domainNegotiator = $domain_negotiator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu.link_tree'), $container
->get('menu.active_trail'), $container
->get('config.factory'), $container
->get('domain.negotiator'));
}
public function getDerivativeId() {
$menu_name = Markup::create($this
->getSetting('menu_name'));
$domain = $this->domainNegotiator
->getActiveDomain();
$domain_domainid = $domain
->getDomainId();
$id = trim($this
->t(DOMAIN_MENUS_MENU_ID_PATTERN, [
'@domain_domainid' => $domain_domainid,
'@menu_name' => $menu_name,
]));
return $id;
}
public function blockForm($form, FormStateInterface $form_state) {
$defaults = $this
->defaultConfiguration();
$options = [
'' => '',
];
$domain_menus_menu_names = $this->configFactory
->get(static::SETTINGS)
->get('domain_menus_menu_names');
if (!empty($domain_menus_menu_names)) {
$menu_names = explode("\r\n", $domain_menus_menu_names);
foreach ($menu_names as $menu_name) {
if (!empty($menu_name)) {
$options[$menu_name] = $menu_name;
}
}
}
$form['menu_name'] = array(
'#type' => 'select',
'#title' => $this
->t('Domain menu name'),
'#default_value' => $this
->getSetting('menu_name'),
'#options' => $options,
'#description' => $this
->t(''),
'#required' => TRUE,
);
return parent::blockForm($form, $form_state);
}
public function blockSubmit($form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
foreach (array_keys($this
->defaultConfiguration()) as $element) {
$this->configuration[$element] = $form_state
->getValue($element);
}
}
}
public function build() {
return parent::build();
}
public function defaultConfiguration() {
$defaults = parent::defaultConfiguration();
$defaults['menu_name'] = '';
return $defaults;
}
public function getSetting($key) {
if (isset($this->settings[$key])) {
return $this->settings[$key];
}
$defaults = $this
->defaultConfiguration();
if (isset($this->configuration[$key])) {
$this->settings[$key] = $this->configuration[$key];
}
else {
$this->settings[$key] = $defaults[$key];
}
return $this->settings[$key];
}
}