Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

drupal 7, i have custom article type called webinar, so i need custom template to show…

  1. User

    drupal 7, i have custom article type called webinar, so i need custom template to show list of this materials of this type under url /webinars, so i make a template page--webinars.tpl.php what code do i need to write inside this template to show list of webinar type articles ordered by pubdate?

  2. Assistant

    To create a custom page template in Drupal 7 that lists nodes of a specific content type ordered by their publication date, you'll need to follow these general steps:

    1. Create the Custom Page Template:

      You've already done this by creating a template file named page--webinars.tpl.php.

    2. Fetch and Display the Nodes in the Template:

      Inside your page--webinars.tpl.php, you need to write PHP code to fetch and display the list of webinar nodes. To do this, you'll use Drupal’s Database API to query the nodes and then render them.

    Here's a detailed example of what your page--webinars.tpl.php might look like:

    <?php
    /**
     * @file
     * Custom page template for displaying a list of webinar nodes.
     */
    
    // Load node objects of 'webinar' content type.
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'webinar')
      ->propertyCondition('status', 1) // ensure that only published nodes are going to be displayed.
      ->propertyOrderBy('created', 'DESC'); // order by publication date descending.
    
    $result = $query->execute();
    
    $nodes = array();
    if (isset($result['node'])) {
      $node_ids = array_keys($result['node']);
      $nodes = node_load_multiple($node_ids);
    }
    
    ?>
    
    <div class="webinars-list">
      <?php if (!empty($nodes)): ?>
        <ul>
          <?php foreach ($nodes as $node): ?>
            <li>
              <h2><?php print l($node->title, 'node/' . $node->nid); ?></h2>
              <div class="node-created"><?php print format_date($node->created, 'custom', 'M d, Y'); ?></div>
            </li>
          <?php endforeach; ?>
        </ul>
      <?php else: ?>
        <p>No webinars found.</p>
      <?php endif; ?>
    </div>
    

    Explanation:

    1. EntityFieldQuery Object: This is used to query entities from Drupal's database, specifically the node entity.

    2. entityCondition: Specifies that you are querying the node entity of the webinar content type.

    3. propertyCondition: Ensures that only published nodes (status = 1) are fetched.

    4. propertyOrderBy: Orders the nodes by the created field (publication date) in descending order.

    5. node_load_multiple: Loads the full node objects for processing and rendering.

    6. Template Output: Loops through the nodes and outputs a list item for each, including a link to the node and the publication date.

    7. Clear Caches:

      Finally, clear your Drupal caches to ensure that your new template is recognized:

      drush cache-clear theme-registry
      

      Or via the admin interface: Administration > Configuration > Development > Performance > Clear all caches.

    This should get you started with displaying a custom list of webinar content type nodes on the /webinars path.