0

I'm trying to create a simple block that have some link fields. those links have some predefined values and after get any new values from the admin replace with values and then render that values to the theme:
my_module\src\Plugin\Block\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 defaultConfiguration() {
    // set default link field value.
    return array(
      'default_LinkA_url' => ('http://www.LinkA.com/'),
      'default_LinkB_url' => ('http://www.LinkB.com/'),
    );
  }

  /**
   * {@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,
    '#default_value' => $this->configuration['default_LinkA_url'],
  );
  $form['block_LinkB_url'] = array(
    '#type' => 'url',
    '#title' => t('Link B:'),
    '#size' => 60,
    '#default_value' => $this->configuration['default_LinkB_url'],
  );
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#markup' => $this->configuration['default_LinkA_url'],
      '#markup' => $this->configuration['default_LinkB_url'],
    ];
  }
}

Whatsoever I try I can't get any result, and this code only show one link like below:
http://www.LinkA.com (a plain text only)

but what I want to achieve is some think like This:
Link A
This Link has Some CSS Style and Open in new page rater than being a plain text like above link.
I would be happy to hear what the problem is.

1

You have to build a render array like this:

   public function build() {
     $build = [];
     $build['linkA'] = [
       '#type' => 'link',
       '#title' => 'Link A',
       '#attributes' => [
         'class' => 'link-class',
         'target' => '_blank',
       ],
       '#url' => Url::fromUri($this->configuration['default_LinkA_url']),
     ];
     $build['linkB'] = [
       // ...
     ];

     return $build;
   }

You can add attributes to the link, classes for css or a target to open it in a new tab.

  • I think it's better to get access the fields data in twig template? How can get access to plugin block (module) fieldes data? – Mojtaba Reyhani Oct 12 '16 at 18:29
  • 1
    You return a render array. You can put predefined render elements in this array like in your example markup and link or your own custom templates or both at the same time. Usually you start with the predefined elements, as long as they do what you want. If you need to customize the output more than the render elements can do, you make your own template(s). But there is no "better". Everyone will do this differently. – 4k4 Oct 12 '16 at 18:44
  • Thank you for the feedback, I've added the $to the code. – 4k4 Oct 13 '16 at 19:33
  • Hi again, I have another question about this topic, Is it possible to get access link element in situation like: <a href="{{ build['linkA'].url }}" class="link-class" target="_blank" > in twig template? Thanks a lot. – Mojtaba Reyhani Oct 15 '16 at 15:33

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.