0

I use the code snippet from: render a drupal 8 entity in a string

$node = \Drupal::entityManager()->getStorage('node')->load($nodeid);
$view_builder = \Drupal::entityManager()->getViewBuilder('node');
$renderarray = $view_builder->view($node, 'full');
$html = \Drupal::service('renderer')->renderRoot($renderarray);

It works in a normal controller class without problems. But I did not get it to work in an Ajax Callback in a Custom Form.

The $html = \Drupal::service('renderer')->renderPlain($build); returns nothing.

Where ist the problem? Here is the reduced code of my test form:

<?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;


class SearchApiPageBlockForm extends FormBase {

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

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

    $form['form']['actions'] = array('#type' => 'actions',);
    $form['form']['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('TESTBUTTON!'),
      '#ajax' => array(
        'callback' => '::myAjaxCallback',
      ),
    );

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
  }


  public function myAjaxCallback(array &$form, FormStateInterface $form_state) {

    $view_builder = \Drupal::entityTypeManager()->getViewBuilder('comment');
    $storage = \Drupal::entityTypeManager()->getStorage('comment');
    $node = $storage->load(103); //cid 103 already exists
    $build = $view_builder->view($node);

    dpm('before rendering'); // -> dpm output is: 'before rendering' 

    $html = \Drupal::service('renderer')->renderPlain($build);  

    dpm($html);// -> dpm has no output 
    dpm('after rendering');// -> dpm has no output

    }

}

My goal is, to return the html of an entity via the

$response->addCommand(new HtmlCommand('#testtmp_message', $output));

UPDATE: Content of build: dpm(build)

enter image description here

  • The code seems to work, on a first quick look. Are you getting any errors (webserver longs or Drupal logger entries) ? – Stefanos Petrakis Jan 30 '17 at 9:41
  • Normally you don't need to render, did you try to use the build array for the ajax command? $response->addCommand(new HtmlCommand('#testtmp_message', $build)); – 4k4 Jan 30 '17 at 9:48
  • @Stefanos Petrakis I have enabled Home > Administration > Configuration > Development "All messages, with backtrace information" and added error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); in my settings.php but there were no errors reported. – userpixel Jan 30 '17 at 12:28
  • @4k4 I tried to return the $build directly but nothing changes: $response->addCommand(new HtmlCommand('#testtmp_message', $build)); I tried a normal return array with success: $build = ['#type' => 'html_tag', '#tag' => 'p', '#value' => 'HERE',]; – userpixel Jan 30 '17 at 12:30
  • @userpixel, what is in $build? - btw. for reporting errors, often you don't see the error on screen when it is an ajax reponse, check the content of the response in the browser debugging tools. – 4k4 Jan 30 '17 at 12:52
0

The code in my question is OK. The problem was caused by a conflict by calling the form from hook_preprocess_field(). I changed the hook, and I am calling the form from template_preprocess_block() and the AJAX callback of the entity gets fine.

4k4 put me on the right track. Thank you.

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.