How to pass multiple block configuration to a behavior in Drupal 8

by riju.srk   Last Updated October 09, 2019 16:07 PM - source

I am trying to create Curator IO module for Drupal8, so I created a block plugin with its own custom config.

public function blockForm($form, FormStateInterface $form_state) {

$form['curator_io_settings'] = array(
  '#type' => 'fieldset',
  '#title' => t('Curator IO Settings'),
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,
);

$form['curator_io_settings']['feed_id'] = [
  '#type' => 'textfield',
  '#title' => $this->t('Enter your unique Feed Id'),
  '#default_value' => $this->configuration['feed_id'],
  '#maxlength' => 256,
  '#size' => 64,
  '#description' => t('Set your unique feed id found at curator io'),
];

return $form;

}

and I attached my library from the build method and passed a variable from here so I can work with it in Jquery

public function build() {

$build = [
  '#theme' => 'curatorio_io',
  '#attached' => [
    'library' => [
      'curatorio/curatorio_assets',
    ],
    'drupalSettings' => [
      'curatorio' => [
        'feed_id' => $this->configuration['feed_id'],
      ]
    ]
  ],
];

return $build;

}

and getting this variable value in JS like this

    (function ($, Drupal, drupalSettings) {
  'use strict';
  Drupal.behaviors.curatorio = {
    attach: function (context, settings) {
     console.log(drupalSettings.curatorio);
    }
  };

It is working fine for one instance of Curator Block.

Problem When I am adding 2 or more instance of this Block, it is not working from 2nd blocks onward, I can understand that no matter how many instance I create for this block the JS will always get included once, so how I will pass multiple feed_id to JS Behaviour? what is the best practice to handle this situation.

Tags : 8 blocks


Related Questions



Create main page content block

Updated April 04, 2015 18:03 PM


Blocks is not showing up

Updated March 29, 2015 06:02 AM