~/drupal/60-drupal-code-fetch-link-title-category-names-tag-names-every-article.md

Drupal Code: Fetch Link, Title, Category names, tag names from every article

1 min readApril 18, 2018by Gorav Singal#drupal#drupal-code#drupal-websites
// TL;DR

Query all node IDs, load each node, and use taxonomy_term_load to fetch category and tag names, outputting a complete article metadata list.

Drupal Code: Fetch Link, Title, Category names, tag names from every article

Problem Statement

You have bunch of articles, say 250 or more. You hired a freelancer to do some SEO stuff. You need to share all the article links, title, tag names etc.

Solution

First run a db query, and fetch all the node ids (nid). For each nid, load the node, and fetch every information that you want to give to the freelancer. For taxonomy terms, load the term by using taxonomy api, and get their names. Similarly for tags.

See the code below:

function getTerms($category) {
  $terms = array();
  foreach($category as $val) {
    $terms[] = $val['tid'];
  }
  $result = taxonomy_term_load_multiple($terms);
  $ret = array();
  foreach($result as $res) {
    $ret[] = $res->name;  
  }
  return implode('|', $ret);
}

$query = db_select( 'node', 'n' );
  $query
    ->condition( 'n.type', 'article' )
    ->fields('n', array('nid'));
  $result = $query
     ->execute()
     ->fetchAll();

  foreach( $result as $row ) {
    $nd = node_load($row->nid);
    $tags = getTerms($nd->field_tags['und']);
    $terms = getTerms($nd->field_article_category['und']);
    print 'https://www.yourwebsite/node/'.$nd->nid.','.'"'.$nd->title.'"'.','.$terms.','.$tags."\n";
  }

The output will be 4 columns separated by comma. You can save this information in a csv and share this file.

Sample output:

https://www.yourwebsite/node/9,"Advantages of AUTOCAD: Discovering The Best",Autocad,
https://www.yourwebsite/node/10,"Auto CAD: 3D Software That Suits Any Sense Of Style Along With Many Benefits",Autocad,
https://www.yourwebsite/node/11,"Auto CAD For Designing Great Automobile Stickers",Autocad,
https://www.yourwebsite/node/12,"AUTO CAD Where Dreams Meet Reality",Autocad,Autocad
https://www.yourwebsite/node/14,"Brand New Building Technologies That Are Here To Stay ",Architecture|Exterior,technology
https://www.yourwebsite/node/15,"Design Ideas To Make Your Hotel “A Pleasant Stay”",Design Ideas|Interior Design,Hotel|Design|ideas
https://www.yourwebsite/node/16,"A Comparative Analysis Between Modern, Contemporary and Traditional Furniture",Furniture|Interior Design,Furniture|Modern Furniture|Contemporary Furniture|Traditional Furniture

Enjoy

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

How to add alt attribute of images in all of my drupal articles or other content type

How to add alt attribute of images in all of my drupal articles or other content type

I have a custom content type, and there are around 2000 number of nodes in my…

Drupal 8: How to Export and Import View

Drupal 8: How to Export and Import View

You have created some views, and want to port it to your production environment…

Drupal 7: How to save a node programmatically and add an image field from a public URL

Drupal 7: How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

Drupal 7: How to implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

Drupal 7: How to implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

hook_cron() suggests to put tasks that executes in shorter time or non-resource…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…