0

I'm attempting to grab all published content of type 'homepage_slide', as currently my slider loads all content unpublished or otherwise.

Here is my current Abstraction query.

  if ($variables['is_front']) {

    // Create data for slider
    // Get from DB nids of content type homepage_slide
    $query = \Drupal::database()->select('node', 'n');
      $query->fields('n', ['nid']);
      $query->condition('n.type', 'homepage_slide');
      $query->condition('n.status', 1);
      $nids = $query->execute()->fetchAll();

    $variables['home_page']['slides'] = array();

    // get and set values from content type to variable
    foreach ($nids as $val) {
      $node = Drupal\node\Entity\Node::load($val->nid);

      $text = $node->get('body')->getValue();
      $img = $node->get('field_image')->getValue();
      $link_url = $node->get('field_button_link')->first()->getUrl();
      $link_title = $node->get('field_button_link')->first()->title;

      $file = \Drupal\file\Entity\File::load($img[0]['target_id']);
      $img_src = $file->url();

      $variables['home_page']['slides'][] = array(
        'text' => ['#markup' => $text[0]['value']],
        'img_src' => $file->url(),
        'link_url' => $link_url,
        'link_title' => $link_title
      );
    }

  }

When I remove the line $query->condition('n.status', 1); it works. However throws the below error when I leave it in.

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Drupal\Core\Database\DatabaseExceptionWrapper</em>: SQLSTATE[42S22]: Column not found: 1054 Unknown column &#039;status&#039; in &#039;where clause&#039;: SELECT n.nid AS nid
FROM
{node} n
WHERE (n.type = :db_condition_placeholder_0) AND (status = :db_condition_placeholder_1); Array
(
    [:db_condition_placeholder_0] =&gt; homepage_slide
    [:db_condition_placeholder_1] =&gt; 1
)
 in <em class="placeholder">themename_preprocess_page()</em> (line <em class="placeholder">126</em> of <em class="placeholder">themes/themename/themename.theme</em>). <pre class="backtrace">Drupal\Core\Database\Statement-&gt;execute(Array, Array) (Line: 631)
Drupal\Core\Database\Connection-&gt;query(&#039;SELECT n.nid AS nid
FROM
{node} n
WHERE (n.type = :db_condition_placeholder_0) AND (status = :db_condition_placeholder_1)&#039;, Array, Array) (Line: 358)
Drupal\Core\Database\Driver\mysql\Connection-&gt;query(&#039;SELECT n.nid AS nid
FROM
{node} n
WHERE (n.type = :db_condition_placeholder_0) AND (status = :db_condition_placeholder_1)&#039;, Array, Array) (Line: 510)
Drupal\Core\Database\Query\Select-&gt;execute() (Line: 126)
themename_preprocess_page(Array, &#039;page&#039;, Array) (Line: 287)
Drupal\Core\Theme\ThemeManager-&gt;render(&#039;page&#039;, Array) (Line: 437)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 501)
Drupal\Core\Template\TwigExtension-&gt;escapeFilter(Object, Array, &#039;html&#039;, NULL, 1) (Line: 176)
__TwigTemplate_e559a627712398e644a3084e1d00b70842705a0f1b596808c95e6be526e73217-&gt;doDisplay(Array, Array) (Line: 455)
Twig\Template-&gt;displayWithErrorHandling(Array, Array) (Line: 422)
Twig\Template-&gt;display(Array) (Line: 434)
Twig\Template-&gt;render(Array) (Line: 64)
twig_render_template(&#039;themes/themename/templates/html.html.twig&#039;, Array) (Line: 384)
Drupal\Core\Theme\ThemeManager-&gt;render(&#039;html&#039;, Array) (Line: 437)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray(Object, &#039;kernel.view&#039;, Object)
call_user_func(Array, Object, &#039;kernel.view&#039;, Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch(&#039;kernel.view&#039;, Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

Thanks in advance!

  • The node table doesn't have a status column in Drupal 8. Use node_field_data or preferably use EntityQuery to build your query. – Alfred Armstrong Nov 4 at 10:25
1

Use an Drupal::entityQuery. The node table doesn't contain the status.

$nids = \Drupal::entityQuery('node')
  ->condition('type','homepage_slide')
  ->condition('status', 1);
  ->execute();

$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

// Loop through all nodes.
foreach ($nodes as $node) {

  // ...
}
  • Thanks for this! To confirm, I straight replace the 5 lines above with the 4 you posted here? – Rob Hern Nov 4 at 10:35
  • @RobHern – Yepp, exactly. Then debug what's inside $nids, for example with Devel's submodule Kint you could do ksm($nids);. And then continue whatever you wanted to do with these node IDs. – leymannx Nov 4 at 10:40
  • Thanks! I have this already in my function, previously working as intended - I've updated the original question with that code now also - but it's throwing an error of AssertionError: Cannot load a NULL ID. in assert() (line 249 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).. Is this due to how I've grabbed the node content? – Rob Hern Nov 4 at 11:00
  • @RobHern – Updated answer. As I said, you can debug what's inside the variables with Kint's ksm($val) etc. – leymannx Nov 4 at 11:15
  • @RobHern – The worst thing about this is that you seem to be doing this from within a preprocess function. Where it actually should be a Views block. And then you don't need this code at all. – leymannx Nov 4 at 11:20

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.