This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/block.twig
2018-11-23 12:29:20 +00:00

120 lines
2.9 KiB
Twig

{% import 'lib/di.twig' as di %}
<?php
namespace Drupal\{{ machine_name }}\Plugin\Block;
{% sort %}
{% if access %}
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
{% endif %}
use Drupal\Core\Block\BlockBase;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
/**
* Provides {{ plugin_label|article|lower }} block.
*
* @Block(
* id = "{{ plugin_id }}",
* admin_label = @Translation("{{ plugin_label }}"),
* category = @Translation("{{ category }}")
* )
*/
class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
{% if services %}
{{ di.properties(services) }}
/**
* Constructs a new {{ class }} instance.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
{{ di.annotation(services) }}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, {{ di.signature(services) }}) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
{{ di.assignment(services) }}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
{{ di.container(services) }}
);
}
{% endif %}
{% if configurable %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'foo' => $this->t('Hello world!'),
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['foo'] = [
'#type' => 'textarea',
'#title' => $this->t('Foo'),
'#default_value' => $this->configuration['foo'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['foo'] = $form_state->getValue('foo');
}
{% endif %}
{% if access %}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
// @DCG Evaluate the access condition here.
$condition = TRUE;
return AccessResult::allowedIf($condition);
}
{% endif %}
/**
* {@inheritdoc}
*/
public function build() {
$build['content'] = [
'#markup' => $this->t('It works!'),
];
return $build;
}
}