0

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.

Quantity of fields

  • Then the form with the amount of fields is created:

enter image description here

  • And this is what my code is rendering at this moment:

enter image description here

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!

  • Maybe you need to define your $block1 variable before your loop block starts? like $block1 = []; – Insasse Feb 8 '18 at 10:25
  • Just tested... but no.. didn't work... – Hawk Feb 8 '18 at 23:50
  • At the public function build() Is it possible to replace the FOR loop with a foreach? I've being trying but haven't made it right i think how would be the right way to do it that way? – Hawk Feb 9 '18 at 0:47
  • Or a foreach nested into a FOR loop.. ? – Hawk Feb 9 '18 at 0:55
0

After hours and hours... i finally found the correct way to implement this for loop at function build()

This is the final code:

<?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() {
    $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++) {
    ${'p' . $i} = $config['fss_content_paragraph'][$i];
    $block1['p' . $i]['#prefix'] = '<p>';
    $block1['p' . $i]['#suffix'] = '</p>';
    $block1['p' . $i]['#markup'] = ${'p' . $i};
  }

  return $block1;

  }//End build
  /**
  * {@inheritdoc}
  */
  public function blockForm($form, FormStateInterface $form_state) {
    $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;

  }//End blockForm
  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    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

Hopefully some day this will help someone else...

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.