Skip to main content
added 4 characters in body
Source Link
Mojtaba Reyhani
  • 2.1k
  • 8
  • 35
  • 67

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.

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->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.

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.

edited tags
Link
Mojtaba Reyhani
  • 2.1k
  • 8
  • 35
  • 67
edited body
Source Link
Mojtaba Reyhani
  • 2.1k
  • 8
  • 35
  • 67

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->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.

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->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.

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->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.

edited title
Link
Mojtaba Reyhani
  • 2.1k
  • 8
  • 35
  • 67
Loading
Source Link
Mojtaba Reyhani
  • 2.1k
  • 8
  • 35
  • 67
Loading