0

I'm attempting to use queries within a block plugin to avoid using views for performance purposes.

namespace Drupal\my_plugins\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Query;

/**
 * @Block(
 *   id = "basic_text",
 *   admin_label = @Translation("Basic Block")
 * )
 */

class Basic extends BlockBase {

    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return array(
            'bodytext-submit' => $this->t('Enter text here'), 
            'layout' => array('pane-type-submit' => 'box'),
            );
    }

    /**
     * {@inheritdoc}
     */
    public function blockForm($form, FormStateInterface $form_state){
        include 'StyleForm.php';

        $form['bodytext'] = array(
          '#type' => 'text_format',
          '#title' => $this->t('Block contents'),
          '#format' => 'full_html',
          '#description' => $this->t('This text will appear in the block.'),
          '#default_value' => $this->configuration['bodytext-submit'],
        );
        return $form;
    }

    public function blockSubmit($form, FormStateInterface $form_state) {
        $this->configuration['bodytext-submit']  = $form_state->getValue('bodytext');
        }

    public function build(){
        $query = \Drupal::entityQuery('node');
        $nids = $query->execute();
        return array(
            '#type' => 'markup',
            '#markup' => $this->configuration['bodytext-submit']['value'],
        );
    }
}

I ultimately want to be able to perform a query and display a list of nodes that fulfill a certain requirement. As a test I performed an empty query for all nodes without displaying any of the results. But when I add

$query = \Drupal::entityQuery('node');
$nids = $query->execute();

to the build() function. It makes it so I can't render the block. I would like to be able to perform entity queries and display the results within the block.

3
  • Can you be more specific which error you get?
    – Maouna
    Commented Jun 22, 2016 at 16:02
  • I don't actually receive an error, it just doesn't do anything when I attempt to add the pane in the panels edit page. Looking at the console within the browser I just see 'Uncaught AjaxError: An AJAX HTTP error occurred'
    – Amy
    Commented Jun 22, 2016 at 17:40
  • 1
    I reproduced your error and got rid of it removing 'include 'StyleForm.php';'
    – Maouna
    Commented Jun 22, 2016 at 18:11

2 Answers 2

2

Sorry Maouna, but don't you need to load the nodes and not just find the nids ? Something like :

$build = [
   '#theme' => 'item_list',
   '#items' => array(),
  ];

    $entity_type = 'node';
    $storage = \Drupal::entityTypeManager()->getStorage($entity_type); 
    $nodes = $storage->loadMultiple($nids);
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
    $view_mode = 'teaser'; // You can change for another view_mode if that suits you. You can considere creating one.

    foreach ($nodes as $node) {
      $build['#items'][] = $view_builder->view($node, $view_mode);
    }

return $build;
2

There are brackets missing:

$nids = $query->execute();

In my block this works:

 public function build() {
   $query = \Drupal::entityQuery('node');
   $nids = $query->execute();

  $build = [
   '#theme' => 'item_list',
   '#items' => $nids,
  ];

 return $build;
}
1
  • I made the correction but I'm still experiencing the same issue.
    – Amy
    Commented Jun 22, 2016 at 15:55

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.