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.