whitelabel-8.x-2.x-dev/src/Plugin/Menu/WhiteLabelToggleLink.php

src/Plugin/Menu/WhiteLabelToggleLink.php
<?php

namespace Drupal\whitelabel\Plugin\Menu;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\whitelabel\WhiteLabelProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a Menu link.
 */
class WhiteLabelToggleLink extends MenuLinkDefault {

  /**
   * The white label manager.
   *
   * @var \Drupal\whitelabel\WhiteLabelProviderInterface
   */
  protected $whiteLabelProvider;

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

  /**
   * Constructs a new WhiteLabelToggleLink.
   *
   * @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\Menu\StaticMenuLinkOverridesInterface $static_override
   *   The static override storage.
   * @param \Drupal\whitelabel\WhiteLabelProviderInterface $white_label_provider
   *   The white label manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, WhiteLabelProviderInterface $white_label_provider, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);

    $this->whiteLabelProvider = $white_label_provider;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('menu_link.static.overrides'),
      $container->get('whitelabel.whitelabel_provider'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Check if the current user does have an associated white label.
   *
   * @return \Drupal\whitelabel\Entity\WhiteLabelInterface||null
   *   The first found white label or NULL.
   */
  private function getUserWhiteLabel(AccountInterface $account = NULL) {
    if (!$account) {
      $account = \Drupal::currentUser();
    }

    if ($whitelabels = $this->entityTypeManager->getStorage('whitelabel')
      ->loadByProperties(['uid' => $account->id()])) {
      return reset($whitelabels);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled() {
    return (bool) $this->getUserWhiteLabel();
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    if ($this->whiteLabelProvider->getWhiteLabel()) {
      return $this->t('@site_name website', ['@site_name' => \Drupal::configFactory()->getEditable('system.site')->get('name')]);
    }
    else {
      return $this->t('My website');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteName() {
    return '<current>';
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteParameters() {
    return \Drupal::routeMatch()->getRawParameters()->all();
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions() {
    $options = parent::getOptions();

    if ($this->whiteLabelProvider->getWhiteLabel()) {
      // Explicitly unset the white label.
      $options['whitelabel'] = FALSE;
    }
    else {
      // Explicitly set the white label. The outbound url processor is also
      // reading the white label option to apply it in the right place.
      $options['whitelabel'] = $this->getUserWhiteLabel();
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return ['whitelabel'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $whitelabel = $this->getUserWhiteLabel();
    if ($whitelabel) {
      return $whitelabel->getCacheTags();
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc