1

I need to create a select field in a config form and pass values to it along with custom #theme options. Its easy to pass header & row values. But after selecting a value from select box, I need to load another dropdown, Below is my reference code:

addslider.php

 public function buildForm(array $form, FormStateInterface $form_state) {
 $vid = 'my_categories';
 $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);

 foreach ($terms as $term) {
   $custom_categories[$term->tid] = str_repeat('-', $term->depth) . $term->name;
 }
  $form['block']['sliders'] = array(
  '#type' => 'fieldset',
  '#title' => $this->t('My Sliders'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#theme'  => 'my_custom_slider_table',
);

foreach ($block['sliders'] as $key => $slider) {

$form['block']['sliders'][$key]['tid'] = array(
  '#type'          => 'select',
  '#title'         => t('Categories'),
  '#options'       => $custom_categories,
  '#required'      => TRUE,
  '#default_value' => !empty($slider['tid']) ? $slider['tid'] : NULL,
);
}

hook_theme() in module file:

function mycustom_theme() {
  return [
    'my_custom_slider_table' => [
    //'variables' => [
    //'header' => $header,
    //'rows' => $example_rows,
    //'attributes' => array('id' => $table_id),
    //],
    'variables' => [
    'slider ' => [],
    ],
  ],
];

}

my-custom-slider-table.html.twig file:

<table>
  <thead>
    <tr>
      <th>{{ 'Taxonomy select' }}</th>
      <th>{{ 'Reference Node' }}</th>
      <th>{{ 'Options' }}</th>
    </tr>
  </thead>
  <tbody>
   <tr> <td>{{ slider }}</td><td>Test Sponsors</td><td>Edit/Delete</td></tr>
 </tbody>
</table>

I need to get the output of $custom_categories variable in twig file, for the first td in {{ slider }} . Can you please help, new to Drupal8.

1 Answer 1

2

You need to add it to your $build render object. Here's a piece of sample code, in this case from a Block plugin with a form on it, similar to yours:

...
in my BlockPlugin
..



public function build() {
    // getConfiguration returns the values from the form I have and save to a config object.
    $source = $this->getConfiguration()['source'];
    $access_token = $this->getConfiguration()['access_token'];

    $build = [
      '#theme' => 'my_fancy_search_widget',
      '#source_data' => $source,
      '#access_token' => $access_token,
    ];
    return $build;
  }


...
in my .module file:
...


/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  return [
  'my_fancy_search_widget' => [
      'template' => 'main-search',
      'variables' => [
        'source_data' => NULL,
        'access_token' => NULL,
      ],
  ];
}


in my twig template I can print these vars with {{ source_data }} and {{ access_token }}

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.