View source
<?php
namespace Drupal\copyright_footer\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
class CopyrightFooter extends BlockBase {
public const VERSION = '8.x-1.x-dev';
public function defaultConfiguration() : array {
return [
'organization_name' => '',
'organization_url' => '',
'year_origin' => '',
'year_to_date' => '',
'version' => '',
'version_url' => '',
'label_display' => FALSE,
];
}
public function blockForm($form, FormStateInterface $form_state) : array {
$form['organization_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Organization name'),
'#default_value' => $this->configuration['organization_name'],
];
$form['organization_url'] = [
'#type' => 'url',
'#title' => $this
->t('Organization URL'),
'#description' => $this
->t('Leave blank if not necessary.'),
'#default_value' => $this->configuration['organization_url'],
];
$form['year_origin'] = [
'#type' => 'textfield',
'#title' => $this
->t('Year origin from'),
'#description' => $this
->t('Leave blank if not necessary.'),
'#default_value' => $this->configuration['year_origin'],
];
$date = new \DateTime();
$form['year_to_date'] = [
'#type' => 'textfield',
'#title' => $this
->t('Year to date'),
'#description' => $this
->t('Leave blank then the current year (@year) automatically shows up.', [
'@year' => $date
->format('Y'),
]),
'#default_value' => $this->configuration['year_to_date'],
];
$form['version'] = [
'#type' => 'textfield',
'#title' => $this
->t('Version'),
'#description' => $this
->t('Leave blank if not necessary.'),
'#default_value' => $this->configuration['version'],
];
$form['version_url'] = [
'#type' => 'url',
'#title' => $this
->t('Version URL'),
'#description' => $this
->t('Leave blank if not necessary. It works w/ the version number above.') . $this
->t("If you don't input the version number, this field will be simply ignored."),
'#default_value' => $this->configuration['version_url'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) : void {
$this->configuration['organization_name'] = $form_state
->getValue('organization_name');
$this->configuration['organization_url'] = $form_state
->getValue('organization_url');
$this->configuration['year_origin'] = $form_state
->getValue('year_origin');
$this->configuration['year_to_date'] = $form_state
->getValue('year_to_date');
$this->configuration['version'] = $form_state
->getValue('version');
$this->configuration['version_url'] = $form_state
->getValue('version_url');
}
public function build() : array {
$date = new \DateTime();
$year_to_date = empty($this->configuration['year_to_date']) ? $date
->format('Y') : $this->configuration['year_to_date'];
$organization = $this->configuration['organization_name'];
if (!empty($organization) && !empty($this->configuration['organization_url'])) {
$url = Url::fromUri($this->configuration['organization_url']);
$organization = Link::fromTextAndUrl($this->configuration['organization_name'], $url)
->toString();
}
$version = $this->configuration['version'];
if (!empty($version)) {
$version = $this
->t('ver.@version', [
'@version' => $version,
]);
if ($this->configuration['version_url']) {
$url = Url::fromUri($this->configuration['version_url']);
$version = $this
->t('ver.@version', [
'@version' => Link::fromTextAndUrl($this->configuration['version'], $url)
->toString(),
]);
}
}
$year = $date
->format('Y');
return empty($this->configuration['year_origin']) || "{$this->configuration['year_origin']}" === $year ? [
'#type' => 'markup',
'#markup' => $this
->t('Copyright © @year @organization @version', [
'@year' => $year,
'@organization' => $organization,
'@version' => $version,
]),
] : [
'#type' => 'markup',
'#markup' => $this
->t('Copyright © @year_origin-@year_to_date @organization @version', [
'@year_origin' => $this->configuration['year_origin'],
'@year_to_date' => $year_to_date,
'@organization' => $organization,
'@version' => $version,
]),
];
}
}