-1

I created a block based on the view with machine name search_entity_product_block:

enter image description here

Then I created a custom form with method someMethod which should render a block of the view below the search field:

class SearchFieldForm extends FormBase {

  public function getFormId() {
    return 'entity_product_admin_search_field';
  }

  public function someMethod($args) {

    $view = [
      '#type' => 'view',
      '#name' => 'search_entity_product',
      '#display_id' => 'search_entity_product_block',
      '#arguments' => $args,
      '#embed' => TRUE,
    ];

    return $view;
  }

  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['search_field'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Search Product'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $args = $form_state->getValue('search_field');
    $this->someMethod($args);
  }

}

But the block didn't render.

2
  • 1
    You are returning a view object in a method that you created yourself, right? Simply returning a view object is not sufficient to render it; you need to tell it to render the view. However, I don't think you can render a block in submitForm(); you'll need to refactor the code to render it somewhere else. Jul 14 at 4:46
  • Yes, the submission handler can only save values. The block needs to be rendered in buildForm(), as 4uk4's answer says.
    – apaderno
    Jul 14 at 8:31

1 Answer 1

1

The submit function needs only to request a form rebuild and then the form build method can place the View block under the search field:

class SearchFieldForm extends FormBase {

  public function getFormId() {
    return 'entity_product_admin_search_field';
  }

  public function someMethod($args) {

    $view = [
      '#type' => 'view',
      '#name' => 'search_entity_product',
      '#display_id' => 'search_entity_product_block',
      '#arguments' => $args,
      '#embed' => TRUE,
    ];

    return $view;
  }

  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['search_field'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Search Product'),
    ];

    $args = $form_state->getValue('search_field');
    if ($args) {
      $form['result'] = $this->someMethod($args);
    }

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setRebuild();
  }

}
1
  • This is how something that depends on the submitted values is rendered together a form. It's the same way used in Drupal 7.
    – apaderno
    Jul 14 at 9:10

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.