0

how can I get access to fields data (Variables) of custom plugin block in twig template?
this block has been created by a module (Not a custom block type) and has its own twig template.
in below is shown structure of my module directory:

my_module
  ├ src
  | └ Plugin
  |    └ Block
  |       └ MyBlock.php
  ├ templates
  |  └ block--my-module.html.twig
  ├ my_module.info.yml
  └ my_module.module

MyBlock.php:

<?php

namespace Drupal\my_modules\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;


/**
 * @Block(
 *   id = "my_block",
 *   admin_label = @Translation("My Block"),
 *   category = @Translation("My Block")
 * )
 */
class MyBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    // Add a form field to the existing block configuration form.

  $form['block_LinkA_url'] = array(
    '#type' => 'url',
    '#title' => t('Link A:'),
    '#size' => 60,
  );
  $form['block_LinkB_url'] = array(
    '#type' => 'url',
    '#title' => t('Link B:'),
    '#size' => 60,
  );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    // Save our custom settings when the form is submitted.
    $this->configuration['LinkA_url'] = $form_state->getValue('block_LinkA_url');
    $this->configuration['LinkB_url'] = $form_state->getValue('block_LinkB_url');
  }

}

I'm trying to get access to for eg LinkA_url Variable in block--my-module.html.twig with something like below code:

<div>
    {% block content %}
      {{ content.LinkA_url.value }}
    {% endblock %}
</div>

But whatsoever I try nothing could not be displayed , I would be happy to hear what the problem is.
Thanks for any help.

1
+50

you can use markup render arrays like this:

 public function build() {
    $build = [];

    $build['LinkA_url'] = ['#markup' => $this->configuration['LinkA_url']];
    $build['LinkB_url'] = ['#markup' => $this->configuration['LinkB_url']];

    return $build;
  }

Then in your twig:

  <p><a href="{{ content.LinkA_url }}">link 1</a></p>
  <p><a href="{{ content.LinkB_url }}">link 2</a></p>

then add hook_theme in your .module file:

/**
 * Implements hook_theme().
 */
function my_module_theme() {
  return array(
    'block__my_module' => array(
      'base hook' => 'block'
    ),
  );
}

Otherwise, it won't look in your module's templates folder.

  • Thanks for your reply, but I get this error message:The website encountered an unexpected error. Please try again later. ParseError: syntax error, unexpected – Mojtaba Reyhani Oct 12 '16 at 17:26
  • 1
    the new array syntax works with PHP 5.4 and greater, see php.net/manual/en/language.types.array.php – oknate Oct 17 '16 at 1:40
  • I have updated the answer to remove new bracket array syntax. – oknate Oct 17 '16 at 1:46
  • Thanks again, all part of that posted in question, I think the problem goes back to the return array, see this post: Render link elements in blocks – Mojtaba Reyhani Oct 19 '16 at 17:42
  • I have tested locally and got it working, see revised answer. – oknate Oct 19 '16 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.