Skip to content
Snippets Groups Projects

Issue#3469612: Fix Hide block/view if No results behavior.

1 file
+ 36
4
Compare changes
  • Side-by-side
  • Inline
@@ -4,13 +4,15 @@ declare(strict_types=1);
namespace Drupal\entity_extra_field\Plugin\ExtraFieldType;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\views\Views;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\entity_extra_field\ExtraFieldTypePluginBase;
use Drupal\views\ViewEntityInterface;
use Drupal\views\Views;
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
/**
* Define extra field views plugin.
@@ -145,6 +147,8 @@ class ExtraFieldViewsPlugin extends ExtraFieldTypePluginBase {
return [];
}
$view = Views::getView($view_name);
$view->setDisplay($this->getViewDisplay());
$view->setArguments($this->getViewArguments($entity));
if ($offset = $this->getViewOffset()) {
$offset = $this->processEntityToken(
@@ -154,6 +158,10 @@ class ExtraFieldViewsPlugin extends ExtraFieldTypePluginBase {
$view->setOffset((int) $offset);
}
if ($this->isViewOutputEmpty($view)) {
return [];
}
return $view->buildRenderable(
$this->getViewDisplay(),
$this->getViewArguments($entity)
@@ -203,6 +211,30 @@ class ExtraFieldViewsPlugin extends ExtraFieldTypePluginBase {
return $this->getConfiguration()['display'] ?? 'default';
}
/**
* Determine if the views display output is empty.
*
* @param \Drupal\views\ViewExecutable $view
* The views executable instance.
*
* @return bool
* Return TRUE if the views display output is empty; otherwise FALSE.
*/
protected function isViewOutputEmpty(
ViewExecutable $view
): bool {
$view->preExecute();
$view->execute();
$display = $view->getDisplay();
if (!$display instanceof DisplayPluginInterface) {
return FALSE;
}
return $display->outputIsEmpty();
}
/**
* Get the view arguments.
*
Loading