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

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->setConfigurationValue['default_LinkA_url']
      = $form_state->getValue('block_LinkA_url');

    $this->setConfigurationValue['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 cant get any result, I would be happy to hear what the problem is.

  • What do you mean by I can't get any result? – kiamlaluno Oct 11 '16 at 3:31
  • @kiamlaluno: Thank you so much for your reply and attention, That's code only show default value and one of the two links. – Mojtaba Reyhani Oct 11 '16 at 6:10
2

If you use setConfigurationValue(), put the value as parameter in the function:

$this->setConfigurationValue('default_LinkA_url', $form_state->getValue('block_LinkA_url'));

Or better set the value in the configuration array like this:

$this->configuration['default_LinkA_url'] = $form_state->getValue('block_LinkA_url');

To display the url use the render element link:

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Link.php/class/Link/8.2.x

  • Thanks a lot for your help, my problem with assign New value properly solved, but I can't render those link yet, I become very happy to help me – Mojtaba Reyhani Oct 11 '16 at 7:51
  • You overwrite the first markup with the second markup. If you put both markups in two different array keys this will work. But better use the link element, as I mentioned in the answer. – 4k4 Oct 11 '16 at 8:51
  • Thank's again, I tried to use link element for render, but I give error only. can you give me finall code for rendering, because I haven't enough information about it. – Mojtaba Reyhani Oct 11 '16 at 10:12
  • Try a new new question: "How to use the link element?" You can give a code example and tell what kind of error you get. – 4k4 Oct 11 '16 at 11:21

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.