2

I have a content type whose human readable name is Post your blog. How can I get the content type machine name by the human readable name?

Is there a function in Drupal 7, or do I have to query the node_type table to get what I want?

  • 1
    There is no function I know of. The point of machine names is to use them and only convert to human ones on read. Human readable names are not unique, so you will have to take care about that. – Mołot Jun 5 '13 at 16:10
3

You can easily build a query to achieve this. However, as Molot mentioned, you have to handle human readable names that aren't unique.


  $machine_name = db_select('node_type', 'n')
  ->fields('n', array('type'))
      ->condition('name', 'Basic page')
      ->execute()->fetchField();
  • @jurgebR thanks for the rely , do i have to write your code like this ? $machine_name = db_query('node_type', 'n') $machine_name->fields('n', array('type') $machine_name->condition('name', 'Post your blog') $machine_name->execute()->fetchField(); – YassineEdouiri Jun 5 '13 at 16:27
  • i found some difficult to understand your code , can you please be more specific ? ^^ – YassineEdouiri Jun 5 '13 at 16:33
  • 1
    My mistake, I've uncorrectly used db_query. Needs to be db_select, updated the answer. Just copy paste and change Basic page to Post your blog. – JurgenR Jun 5 '13 at 16:34
  • :-) My pleasure! – JurgenR Jun 5 '13 at 16:38
  • And for folks that came here looking for the reverse: i.e. how to get the human readable name from the machine name: here's 1) how to get the $node object from within search-result--node.tpl.php: $variables['result']['node'] - drupal.org/comment/reply/535578/6186406 and 2) with the $node object, get the type in machine name form and its human name equivalent: node_type_get_name($node) - api.drupal.org/api/drupal/modules%21node%21node.module/function/… – therobyouknow Sep 4 '13 at 16:18
2

Look at node_type_get_types for D7 and simply foreach looking for $object->name as Post your blog.

  • i've tried the node_type_get_types function , it didn't work for my case – YassineEdouiri Jun 5 '13 at 16:31

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.