"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php" (26 Nov 2020, 6560 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SystemBrandingBlock.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\system\Plugin\Block;
    4 
    5 use Drupal\Core\Block\BlockBase;
    6 use Drupal\Core\Cache\Cache;
    7 use Drupal\Core\Config\ConfigFactoryInterface;
    8 use Drupal\Core\Form\FormStateInterface;
    9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
   10 use Drupal\Core\Url;
   11 use Symfony\Component\DependencyInjection\ContainerInterface;
   12 
   13 /**
   14  * Provides a block to display 'Site branding' elements.
   15  *
   16  * @Block(
   17  *   id = "system_branding_block",
   18  *   admin_label = @Translation("Site branding"),
   19  *   forms = {
   20  *     "settings_tray" = "Drupal\system\Form\SystemBrandingOffCanvasForm",
   21  *   },
   22  * )
   23  */
   24 class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInterface {
   25 
   26   /**
   27    * Stores the configuration factory.
   28    *
   29    * @var \Drupal\Core\Config\ConfigFactoryInterface
   30    */
   31   protected $configFactory;
   32 
   33   /**
   34    * Creates a SystemBrandingBlock instance.
   35    *
   36    * @param array $configuration
   37    *   A configuration array containing information about the plugin instance.
   38    * @param string $plugin_id
   39    *   The plugin_id for the plugin instance.
   40    * @param mixed $plugin_definition
   41    *   The plugin implementation definition.
   42    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   43    *   The factory for configuration objects.
   44    */
   45   public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
   46     parent::__construct($configuration, $plugin_id, $plugin_definition);
   47     $this->configFactory = $config_factory;
   48   }
   49 
   50   /**
   51    * {@inheritdoc}
   52    */
   53   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
   54     return new static(
   55       $configuration,
   56       $plugin_id,
   57       $plugin_definition,
   58       $container->get('config.factory')
   59     );
   60   }
   61 
   62   /**
   63    * {@inheritdoc}
   64    */
   65   public function defaultConfiguration() {
   66     return [
   67       'use_site_logo' => TRUE,
   68       'use_site_name' => TRUE,
   69       'use_site_slogan' => TRUE,
   70       'label_display' => FALSE,
   71     ];
   72   }
   73 
   74   /**
   75    * {@inheritdoc}
   76    */
   77   public function blockForm($form, FormStateInterface $form_state) {
   78     // Get the theme.
   79     $theme = $form_state->get('block_theme');
   80 
   81     // Get permissions.
   82     $url_system_theme_settings = new Url('system.theme_settings');
   83     $url_system_theme_settings_theme = new Url('system.theme_settings_theme', ['theme' => $theme]);
   84 
   85     if ($url_system_theme_settings->access() && $url_system_theme_settings_theme->access()) {
   86       // Provide links to the Appearance Settings and Theme Settings pages
   87       // if the user has access to administer themes.
   88       $site_logo_description = $this->t('Defined on the <a href=":appearance">Appearance Settings</a> or <a href=":theme">Theme Settings</a> page.', [
   89         ':appearance' => $url_system_theme_settings->toString(),
   90         ':theme' => $url_system_theme_settings_theme->toString(),
   91       ]);
   92     }
   93     else {
   94       // Explain that the user does not have access to the Appearance and Theme
   95       // Settings pages.
   96       $site_logo_description = $this->t('Defined on the Appearance or Theme Settings page. You do not have the appropriate permissions to change the site logo.');
   97     }
   98     $url_system_site_information_settings = new Url('system.site_information_settings');
   99     if ($url_system_site_information_settings->access()) {
  100       // Get paths to settings pages.
  101       $site_information_url = $url_system_site_information_settings->toString();
  102 
  103       // Provide link to Site Information page if the user has access to
  104       // administer site configuration.
  105       $site_name_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
  106       $site_slogan_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
  107     }
  108     else {
  109       // Explain that the user does not have access to the Site Information
  110       // page.
  111       $site_name_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
  112       $site_slogan_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
  113     }
  114 
  115     $form['block_branding'] = [
  116       '#type' => 'fieldset',
  117       '#title' => $this->t('Toggle branding elements'),
  118       '#description' => $this->t('Choose which branding elements you want to show in this block instance.'),
  119     ];
  120     $form['block_branding']['use_site_logo'] = [
  121       '#type' => 'checkbox',
  122       '#title' => $this->t('Site logo'),
  123       '#description' => $site_logo_description,
  124       '#default_value' => $this->configuration['use_site_logo'],
  125     ];
  126 
  127     $form['block_branding']['use_site_name'] = [
  128       '#type' => 'checkbox',
  129       '#title' => $this->t('Site name'),
  130       '#description' => $site_name_description,
  131       '#default_value' => $this->configuration['use_site_name'],
  132     ];
  133     $form['block_branding']['use_site_slogan'] = [
  134       '#type' => 'checkbox',
  135       '#title' => $this->t('Site slogan'),
  136       '#description' => $site_slogan_description,
  137       '#default_value' => $this->configuration['use_site_slogan'],
  138     ];
  139     return $form;
  140   }
  141 
  142   /**
  143    * {@inheritdoc}
  144    */
  145   public function blockSubmit($form, FormStateInterface $form_state) {
  146     $block_branding = $form_state->getValue('block_branding');
  147     $this->configuration['use_site_logo'] = $block_branding['use_site_logo'];
  148     $this->configuration['use_site_name'] = $block_branding['use_site_name'];
  149     $this->configuration['use_site_slogan'] = $block_branding['use_site_slogan'];
  150   }
  151 
  152   /**
  153    * {@inheritdoc}
  154    */
  155   public function build() {
  156     $build = [];
  157     $site_config = $this->configFactory->get('system.site');
  158 
  159     $build['site_logo'] = [
  160       '#theme' => 'image',
  161       '#uri' => theme_get_setting('logo.url'),
  162       '#alt' => $this->t('Home'),
  163       '#access' => $this->configuration['use_site_logo'],
  164     ];
  165 
  166     $build['site_name'] = [
  167       '#markup' => $site_config->get('name'),
  168       '#access' => $this->configuration['use_site_name'],
  169     ];
  170 
  171     $build['site_slogan'] = [
  172       '#markup' => $site_config->get('slogan'),
  173       '#access' => $this->configuration['use_site_slogan'],
  174     ];
  175 
  176     return $build;
  177   }
  178 
  179   /**
  180    * {@inheritdoc}
  181    */
  182   public function getCacheTags() {
  183     return Cache::mergeTags(
  184       parent::getCacheTags(),
  185       $this->configFactory->get('system.site')->getCacheTags()
  186     );
  187   }
  188 
  189 }