+{% 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 Drupal\Core\Routing\RouteMatchInterface;
-use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
+ {% endif %}
+{% endsort %}
/**
- * Provides a '{{ plugin_label }}' block.
+ * Provides {{ plugin_label|article|lower }} block.
*
* @Block(
* id = "{{ plugin_id }}",
* category = @Translation("{{ category }}")
* )
*/
-class {{ class }} extends BlockBase implements ContainerFactoryPluginInterface {
+class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
- /**
- * The route match.
- *
- * @var \Drupal\Core\Routing\RouteMatchInterface
- */
- protected $routeMatch;
+{% if services %}
+{{ di.properties(services) }}
/**
* Constructs a new {{ class }} instance.
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
- * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
- * The route match.
+{{ di.annotation(services) }}
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, {{ di.signature(services) }}) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->routeMatch = $route_match;
+{{ di.assignment(services) }}
}
/**
$configuration,
$plugin_id,
$plugin_definition,
- $container->get('current_route_match')
+{{ di.container(services) }}
);
}
+{% endif %}
+{% if configurable %}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
- 'content' => $this->t('Hello world!'),
+ 'foo' => $this->t('Hello world!'),
];
}
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
- $form['content'] = [
+ $form['foo'] = [
'#type' => 'textarea',
- '#title' => $this->t('Block content'),
- '#default_value' => $this->configuration['content'],
+ '#title' => $this->t('Foo'),
+ '#default_value' => $this->configuration['foo'],
];
return $form;
}
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
- $this->configuration['content'] = $form_state->getValue('content');
+ $this->configuration['foo'] = $form_state->getValue('foo');
}
+{% endif %}
+{% if access %}
/**
* {@inheritdoc}
- *
- * @DCG Remove this method of you do not need any access restrictions.
*/
protected function blockAccess(AccountInterface $account) {
- $route_name = $this->routeMatch->getRouteName();
- // Display the block only for anonymous users.
- if ($account->isAnonymous() && $route_name != 'user.register') {
- return AccessResult::allowed()
- ->addCacheContexts(['route.name', 'user.roles:anonymous']);
- }
- return AccessResult::forbidden();
+ // @DCG Evaluate the access condition here.
+ $condition = TRUE;
+ return AccessResult::allowedIf($condition);
}
+{% endif %}
/**
* {@inheritdoc}
*/
public function build() {
$build['content'] = [
- '#markup' => $this->configuration['content'],
+ '#markup' => $this->t('It works!'),
];
return $build;
}