Skip to content

Instantly share code, notes, and snippets.

@jasonawant
Created April 17, 2017 11:31
Show Gist options
  • Save jasonawant/1c7d5f15e97c3e45ad416c1e0415d813 to your computer and use it in GitHub Desktop.
Save jasonawant/1c7d5f15e97c3e45ad416c1e0415d813 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\project_core\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'Project Footer' Block.
*
* @Block(
* id = "project_footer",
* admin_label = @Translation("Platform Footer"),
* )
*/
class ProjectFooterBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* 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 mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$copyright = $this->configFactory->get('project_core.settings')->get('copyright');
$form['footer'] = [
'#type' => 'fieldset',
'#title' => $this->t('Footer elements'),
'#description' => $this->t('Update footer elements here. Changes will be reflected on the <a href="@config">project configuration form</a>.', ['@config' => '/admin/config/system/project']),
];
$form['footer']['copyright'] = [
'#type' => 'textarea',
'#rows' => 3,
'#title' => $this->t('Copyright'),
'#default_value' => $copyright,
'#description' => $this->t('Provide copy to display as a copyright statement in the footer.'),
'#attributes' => [
'placeholder' => '© ' . date("Y") . ' ' . $this->t('Contra legem facit qui id facit quod lex prohibet.'),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
// Retrieve the configuration, set values and save config.
$footer = $form_state->getValue('footer');
$this->configFactory->getEditable('project_core.settings')
->set('copyright', $footer['copyright'])
->save();
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$copyright = $this->configFactory->get('project_core.settings')->get('copyright');
$build['copyright'] = [
'#markup' => '<p>' . $copyright . '</p>',
];
return $build;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return Cache::mergeTags(
parent::getCacheTags(),
$this->configFactory->get('project_core.settings')->getCacheTags()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment