Skip to content

Instantly share code, notes, and snippets.

@larowlan
Last active August 29, 2015 13:56
Show Gist options
  • Save larowlan/9181014 to your computer and use it in GitHub Desktop.
Save larowlan/9181014 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\system\Plugin\Block\SystemBrandingBlock.
*/
namespace Drupal\system\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Component\Utility\Xss;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a block to display 'Site branding' elements.
*
* @Block(
* id = "system_branding_block",
* admin_label = @Translation("Site branding")
* )
*/
class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Creates a SystemBrandingBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
* @param \Symfony\Component\HttpFoundation\Request
* The current request.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory, UrlGeneratorInterface $url_generator, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->urlGenerator = $url_generator;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('url_generator'),
$container->get('request')
);
}
/**
* Generates a URL or path for a specific route based on the given parameters.
*
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* details on the arguments, usage, and possible exceptions.
*
* @return string
* The generated URL for the given route.
*/
public function url($route_name, $route_parameters = array(), $options = array()) {
return $this->urlGenerator->generateFromRoute($route_name, $route_parameters, $options);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'use_site_logo' => TRUE,
'use_site_name' => TRUE,
'use_site_slogan' => TRUE,
'label_display' => FALSE,
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, &$form_state) {
$form['block_branding'] = array(
'#type' => 'details',
'#title' => $this->t('Toggle branding elements'),
'#description' => $this->t('Choose which branding elements you want to show in this block instance.'),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
);
$form['block_branding']['use_site_logo'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Site logo'),
'#description' => $this->t('Defined on the <a href="@appearance">Appearance</a> or theme settings page.', array('@appearance' => $this->url('system.themes_page'))),
'#default_value' => $this->configuration['use_site_logo'],
);
$form['block_branding']['use_site_name'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Site name'),
'#description' => $this->t('Defined on the <a href="@information">Site Information</a> page.', array('@information' => $this->url('system.site_information_settings'))),
'#default_value' => $this->configuration['use_site_name'],
);
$form['block_branding']['use_site_slogan'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Site slogan'),
'#description' => $this->t('Defined in Site information settings.'),
'#default_value' => $this->configuration['use_site_slogan'],
);
// Get the theme.
$theme = $this->request->attributes->get('theme');
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['use_site_logo'] = $form_state['values']['block_branding']['use_site_logo'];
$this->configuration['use_site_name'] = $form_state['values']['block_branding']['use_site_name'];
$this->configuration['use_site_slogan'] = $form_state['values']['block_branding']['use_site_slogan'];
}
/**
* {@inheritdoc}
*/
public function build() {
$build = array();
$site_config = $this->configFactory->get('system.site');
$build['#theme'] = 'branding';
$build['#use_site_logo'] = $this->configuration['use_site_logo'];
$build['#site_logo'] = theme_get_setting('logo');
$build['#use_site_name'] = $this->configuration['use_site_name'];
$build['#site_name'] = $site_config->get('name');
$build['#use_site_slogan'] = $this->configuration['use_site_slogan'];
$build['#site_slogan'] = Xss::filterAdmin($site_config->get('slogan'));
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment