Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / block.twig
index be9280e2dc8f1b034a1e309a87eb1e203884dfee..ab96052a9058961bc3013375114761109976d614 100644 (file)
@@ -1,17 +1,26 @@
+{% 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 }}",
@@ -19,14 +28,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *   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.
@@ -40,12 +45,11 @@ class {{ class }} extends BlockBase implements ContainerFactoryPluginInterface {
    *   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) }}
   }
 
   /**
@@ -56,16 +60,18 @@ class {{ class }} extends BlockBase implements ContainerFactoryPluginInterface {
       $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!'),
     ];
   }
 
@@ -73,10 +79,10 @@ class {{ class }} extends BlockBase implements ContainerFactoryPluginInterface {
    * {@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;
   }
@@ -85,30 +91,27 @@ class {{ class }} extends BlockBase implements ContainerFactoryPluginInterface {
    * {@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;
   }