Drupal 7

Programatically delete taxonomy terms Drupal 7

By calling function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL), an array of all term objects is returned. Extra parameters can be added to the function to select all items belonging to a particular parent or limit depth of the tree. By calling function taxonomy_term_delete($tid), a new db_transaction() starts and the taxonomy object and all its children are deleted from the taxomy_term_data and taxonomy_term_hierarchy tables. As a taxonomy term is an entity which has attached fields, all attached field data is also deleted. Finally, the function clears all static cache variables for terms by using taxonomy_terms_static_reset().

What is Drupal

Drupal is one of the most popular CMS (content management system) around. One of the reasons is that with Drupal, editors and site administrators don't need a technical background to manage and insert content to a site. Possibilities for sites are endless.

Add an extra view mode to an entity

The extra view mode is added to the entity by using hook_entity_info_alter.

In this example the node entity is expanded so a hook_preprocess_hook theme function is used to add all available theme modes as template suggestions to the node. Templates like node--article--teaser.tpl.php and node--article--teaser-overview.tpl.php are now available.

Write your first Drupal 7 module

There are two files needed when building modules:

A .info file which stores metadata about the module to notify Drupal of the existance of a module and renders its information on the administration pages and a .module file where the PHP code is stored.

Remove the taxonomy description field Drupal 7

The description field from taxonomy terms is a property of the taxonomy term entity and can't be deleted. It isn't a field coming from the field module but its values are stored inside the taxonomy_term_data table. Use hook_form_id_alter(&form, &form_state, $form_id) to access the taxonomy term form. This hook is placed inside a custom module.

Programatically add taxonomy vocabularies

In this example a tax_creator module is used to install vocabularies when activating the module and to delete the vocabularies when uninstalling. This is really timesaving when dealing with many vocabularies as the only thing needed to do is to build a vocabulary array and activate the module. Comment hook_uninstall when uninstalling so taxonomy categories are kept. 

Programatically create taxonomy terms

Writing code to create multiple taxonomy items without a parent is really easy. Function taxonomy_term_save($term) requires an object as parameter. An array containing the term name and the vocabulary where to insert is parsed as an object and added as argument. This example shows inserting data in its simplest form, only a name and a vocabulary id is needed.

Remove the read more and comment links on nodes

MYTHEME_preprocess_node is the best solution as it is a theme adjustement. However, when you're using a default Drupal theme, then instead of hacking core, use hook_node_view_alter(&build) inside a custom module.


Subscribe to RSS - Drupal 7