0

I'm working on a view block in which I load several nodes.
I don't know how many nodes will be loaded because it depends on an exposed filter.
I have twig file for the display, I also have a twigExtension php file to call custom functions.

For example, the output of my view looks like this :

Reference  
ABCD  
BCDE  
CDEF  

I would like to get a form :

Reference | Quantity (Select dropdown)
ABCD | 0
BCDE | 1
CDEF | 0
<Submit Button>

Once the user clicks on the Submit Button, it creates 3 nodes of a Custom Content Type.

The lead I'm investigating for now is to use a form but I don't know how I could pass the results to this form, build it with these results and get something like :

public function buildForm(array $form, FormStateInterface $form_state) {  
    $form['ref_1'] = array(
        '#type' => 'textfield',
        '#title' => t('Reference 1'),
        '#default_value' => 'ABCD'
        '#attributes' => array('readonly' => 'readonly')
    );
    $form['quantity_1'] = array(
        '#type' => 'select',
        '#title' => t('Quantity 1'),
        '#options' => array('00', '01', '02', '03'),
    );

    $form['ref_2'] = array(
        '#type' => 'textfield',
        '#title' => t('Reference 2'),
        '#default_value' => 'BCDE'
        '#attributes' => array('readonly' => 'readonly')
    );
    $form['quantity_2'] = array(
        '#type' => 'select',
        '#title' => t('Quantity 2'),
        '#options' => array('00', '01', '02', '03'),
    );

    $form['ref_3'] = array(
        '#type' => 'textfield',
        '#title' => t('Reference 3'),
        '#default_value' => 'CDEF'
        '#attributes' => array('readonly' => 'readonly')
    );
    $form['quantity_3'] = array(
        '#type' => 'select',
        '#title' => t('Quantity 3'),
        '#options' => array('00', '01', '02', '03'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );

    return $form;
}

Or maybe should I look at the exposed filter function, maybe is there a way to modify the way it works to call my own function before display ?

How would you do it ?

Thanks.

0

Build the custom form from inside the view and attach it to the view before rendering:

mymodule.module

/**
 * Implements hook_views_pre_render().
 */
function mymodule_views_pre_render($view) {
  if ($view->id() == 'myview') {
    // get field values from the view result
    $values = [];
    foreach ($view->result as $row_id => $row) {
      foreach ($view->field as $field_id => $field ) {
        $values[$row_id][$field_id] = $field->getValue($row);
      }
    }
    // build form with the view result
    $form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\MyForm', $values);
    // attach the form to the view
    $view->attachment_after = $form;
  }
}

Use the view result for building the form:

/src/Form/MyForm.php

class MyForm extends FormBase {

  public function buildForm(array $form, FormStateInterface $form_state, array $values = NULL) {
    // build form with $values
  }

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.