Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
902 B
2
Indexable
Never
use Drupal\Core\Database\Database;

// Define the node type and node ID you want to check.
$node_type = 'article'; // Replace with your desired content type.
$node_id = 123; // Replace with the node ID you want to check.

// Get the database connection.
$connection = Database::getConnection();

// Build and execute the database query.
$query = $connection->select('node_field_data', 'n')
  ->fields('n', ['status'])
  ->condition('n.type', $node_type)
  ->condition('n.nid', $node_id)
  ->execute();

// Fetch the result.
$result = $query->fetchObject();

if ($result) {
  // Node exists, and you can check its status.
  if ($result->status) {
    // Node is published.
    // Your code for published nodes here.
  } else {
    // Node is unpublished.
    // Your code for unpublished nodes here.
  }
} else {
  // Node with the specified ID and type does not exist.
}