Currently working on a custom module and I’m trying to render a set of arrays that are created through a blockForm and a blockSubmit this ones are working correctly. blockForm is creating the fields of the form correctly and blockSubmit is saving the data of the fields correctly. I tested this several times with positive results. And this two are working with a FOR loop.
Now i’m trying to render the data of this blockForm with public function build()
and a FOR loop. But is only rendering the first array and as it is configurated at the moment is should be four arrays.
This is the code:
FssContentBlock.php
<?php namespace Drupal\fss_content\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Form\FormStateInterface; /** * Provides a 'Fussion Content Block' Block. * * @Block( * id = "fss_content", * admin_label = @Translation("Fussion Content Block"), * category = @Translation("Fussion Content Block"), * ) */ class FssContentBlock extends BlockBase implements BlockPluginInterface { /** * {@inheritdoc} */ public function build() {//ONLY RENDER FIRST ARRAY... $ config = $ this->getConfiguration(); $ config2 = \Drupal::config('fss_content.settings'); $ config2->get('fss_content.paragraphs'); $ qtty = $ config2->get('fss_content.paragraphs'); for ($ i = 0; $ i < $ qtty; $ i ++) { $ i = $ config['fss_content_paragraph'][$ i]; $ block1 = array ( $ i => array ( '#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@i', array('@i' => $ i,)), ), ); } $ block2 = array ( 'test' => array ( '#markup' => $ qtty . '<p>' . $ i . '</p>', //TESTING $ qqty VALUE. IT IS CORRECT... BUT WEIRD $ i VALUE... ) ); $ allblocks = array_merge($ block1, $ block2); return $ allblocks; /* //THIS CODE WORK... BUT NEED IT TO WORK WITH (FOR - LOOPS) $ p0 = $ config['fss_content_paragraph']['0']; $ p1 = $ config['fss_content_paragraph']['1']; $ p2 = $ config['fss_content_paragraph']['2']; $ p3 = $ config['fss_content_paragraph']['3']; $ block = array ( 'p0' => array ( '#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@p0', array('@p0' => $ p0,)), ), 'p1' => array ( '#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@p1', array('@p1' => $ p1,)), ), 'p2' => array ( '#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@p2', array('@p2' => $ p2,)), ), 'p3' => array ( '#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@p3', array('@p3' => $ p3,)), ) ); return $ block; */ }//End build /** * {@inheritdoc} */ public function blockForm($ form, FormStateInterface $ form_state) { //THIS WORK... $ form = parent::blockForm($ form, $ form_state); $ config = $ this->getConfiguration(); $ config2 = \Drupal::config('fss_content.settings'); $ config2->get('fss_content.paragraphs'); $ qtty = $ config2->get('fss_content.paragraphs'); $ form['fss_content_paragraph'] = array( '#type' => 'fieldset', '#tree' => TRUE, ); for ($ i = 0; $ i < $ qtty; $ i ++) { $ num = $ i + 1; $ form['fss_content_paragraph'][$ i] = array( '#type' => 'textarea', '#title' => 'Parrafo N°' . $ num, '#default_value' => isset($ config['fss_content_paragraph'][$ i]) ? $ config['fss_content_paragraph'][$ i] : '', ); } return $ form; } /** * {@inheritdoc} */ public function blockSubmit($ form, FormStateInterface $ form_state) { //THIS WORK... parent::blockSubmit($ form, $ form_state); $ values = $ form_state->getValues(); $ config2 = \Drupal::config('fss_content.settings'); $ config2->get('fss_content.paragraphs'); $ qtty = $ config2->get('fss_content.paragraphs'); for ($ i = 0; $ i < $ qtty; $ i ++) { $ this->configuration['fss_content_paragraph'] = $ form_state->getValue('fss_content_paragraph',$ i); } }//End blockSubmit }//End Class
Explanation of how the module work:
- The module has a configuration option that sets the amount of fields the Administrator user needs.
- Then the form with the amount of fields is created:
- And this is what my code is rendering at this moment:
As you can see is rendering the first array but nothing else.
Now, if you take a look at the test i’m making the $ qtty
value is correct witch is the value of the amount of fields i need.
But the last value of $ i
is weird (last_value_$ i = “ABCE”). Because is taking the first three characters of the first array (First_array = “ABCD”) and the first character of my second array (Second_array= “EFGH”)
Any ideas how can i solve this problem? Thank you!