Drupal 8 foreach and twig templating confusion

Hello,

I’m really confused with twig templating sytem in Drupal 8 and how to bring data on it.

I have built a module that brings data in a block. The thing is I can’t display them normally. I only get the last field.

The build() from the module

  public function build() {
    $config = $this->getConfiguration();
    $getResult = $this->DatabaseResult();

    print_r($getResult);

    $data = [];
    foreach ($getResult as $key) {
      $data = [
        'fid' => $key['fid'],
        'filename' => $key['filename']
      ];

    }

    return array (
      '#theme' => 'mymodule',
      '#title' => $data['fid'],
      '#filename'  => $data['filename'] //Comes from the form below
    );
  }

The result from the print_r from above

The custom.module code

function mymodule_theme($existing, $type, $theme, $path) {
  return array('mymodule' =>
    array(
      'variables' => array(
      'title' => [],
      'filename' => [],
      'name' => null
      ),
      'template' => 'block--helloblock'
    )
  );
}

And the twig template

<div>
  {{ title_prefix }}
  {{ title_suffix }}
    <div>
      <h2>Result</h2>
      {% for unique in title %}
        <div>
          {{unique}}
          {{filename}}
        </div>
      {% endfor %}
    </div>
</div>

Thank you in advance

I don’t know if fixing this will solve the problem, but it can’t hurt.

    foreach ($getResult as $key) {
      $data = [
        'fid' => $key['fid'],
        'filename' => $key['filename']
      ];

    } 

Every time it loops the $data value will become a different array. So whatever the last “each” is, that will be what $data is. I’m betting that isn’t what you want else it would be a “pop”.

I think what you want there is

    foreach ($getResult as $key) {
      $data[] = [
        'fid' => $key['fid'],
        'filename' => $key['filename']
      ];

    } 
1 Like

Nope it gives undefined index fid and undefined index filename in the lines.

        'fid' => $key['fid'],
        'filename' => $key['filename']

Also I changed the twig to

    <div>
      <h2>Result</h2>
      {% for unique in title %}
        <div>
          {{dump(unique)}}
        </div>
      {% endfor %}
    </div>

And I m getting

array(1) { ["fid"]=&gt; string(3) "184" }

array(1) { ["fid"]=&gt; string(3) "191" }

array(1) { ["fid"]=&gt; string(1) "6" }

array(1) { ["fid"]=&gt; string(3) "625" }

array(1) { ["fid"]=&gt; string(3) "189" }

I knew it had something to do with the twig. The new code with errors which I m so close.

  public function build() {
    $config = $this->getConfiguration();
    $getResult = $this->DatabaseResult();

    $data = $getResult;

    return array (
      '#theme' => 'mymodule',
      '#title' => $data
    );
  }

  public function DatabaseResult() {
    // Returns a Drupal\Core\Database\Connection object.
    $connection = \Drupal\Core\Database\Database::getConnection('default', 'migrate');
    $result = $connection->query('
      SELECT
        fld.fid,
        fld.filename,
        fld.filemime,
        fld.filesize,
        fld.status,
        fld.timestamp
      FROM
        {files} fld
    ');
    if ($result) {
       foreach ($result as $record) {
         $fields[] =[
           'fid' => $record->fid,
           'filename' => $record->filename
         ];
       }
    }
    return $fields;
  }

And the twig which brings everything normally

    <div>
      <h2>Result</h2>
      {% for unique in title %}
        <div style="border:1px solid black;padding:20px;">
          <div>Title{{unique['fid']}}</div>
          <div>Filename {{unique['filename']}}</div>
        </div>
      {% endfor %}
    </div>

The custom.module

function hello_world_theme($existing, $type, $theme, $path) {
  return array('mymodule' =>
    array(
      'variables' => array(
      'name' => null
      ),
      'template' => 'block--helloblock'
    )
  );
} 

And above the Result which comes normally
User error : “fid” is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php ).

User error : “filename” is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php ).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.