1

I have create a block with a form and stored something i called 'submit_action_amu_dud_form_block_settings'

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

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

 }

in a method of the same class i can retrieve the value

      $config = $this->getConfiguration();
      $my_stored value=$config['submit_action_amu_dud_form_block_settings']

how can i get it from . module hook_form_alter callback ?

    function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {


if ($form_id == 'amu_dud_form') {
    $form['actions']['submit']['#submit'][] = 'my_submit_method';        
}
}

 function my_submit_method(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

//$my_stored_value=??

2 Answers 2

2

You need to load the block if your outside of the class, for that you need it's id.

$block = \Drupal\block\Entity\Block::load($bid);
$my_stored_value = $block->get('settings')['submit_action_amu_dud_form_block_settings'];

The id can be found in it's annotation if it's custom: @Block( * id = "my_fancy_block_id". If there are several instances of the block this may be altered and can be looked up in the DB, or the block config admin section - check the urls there.

4
  • do u know how to get $bid ? is it no what i have defined in the block class comment* @Block( * id = "amu_dud_form_block",
    – Matoeil
    Oct 19, 2016 at 15:30
  • yes - it is - ;)
    – rémy
    Oct 19, 2016 at 15:35
  • the $block->configuration(....) does not work for instance of block $block = \Drupal\block\Entity\Block::load('amududformblock_3');
    – Matoeil
    Oct 19, 2016 at 15:51
  • and is it not possible to load the configuration for the my_fancy_block_id loaded in the page? (not a static value put in the module code )
    – Matoeil
    Oct 19, 2016 at 15:52
0

thanks to remy i have been able to retrieve the value from an instance of block this way:

//instance of block bid found in db
$block = \Drupal\block\Entity\Block::load('my_blockinstance_bid');
$block_settings = $block->get('settings');
$url=$block_settings['my_stored_setting'];

The problem remain then , when having mutliple instance of blocs, to load a block config dynamically corresponding to the settings value that have been stored for the block being displayed on the page ?

1
  • every instance should have an unique id - thanks for the working code, I updated my answer too, as I just 'guessed'
    – rémy
    Oct 19, 2016 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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