How To Replace The Title Field To a Term Of Reference (Taxonomies)?

How to use Drupal Rules module to automatically set a value of a field.

April 15, 2015
php development

How can you replace the title field to a term of reference (taxonomies)? This is very tricky because the title field is been hardcore written on a content type (in Drupal 7) and by default it cannot be easily replace. In this article I would explain you how I was able to use a term of reference as my title. But first why use a term of reference instead of a title?

These are the reasons why I need it to use term reference instead of title field:

  • Facets - I’m building a system with Search Api + Views + Facets. And I wanted to use the title field on one of the Facets. If you used the title field facets would recognize it as Full Text and it would display it very weird with lower cases and separate words.
  • Standardize – I was building a system where the title is going to be already setup by the system administrator so we could not allow user to enter any item.
  • Autocomplete - If I’m using the term of reference I can integrate Autocomplete Deluxe module to have a nice features to autocomplete the tile.

Warning This content type it was not build for blogging purpose or any search engine optimization (SEO) purpose. Actually this is the goes against SEO best practices. However, these pages (nodes) are not been crawl by any search engines because is only for register user only.

The obvious solution is going to be to find a way to disable the title field from the content type. This can be easily done by either by a custom module, Title field module or the Title field UI it would works as well. By disabling this title field you can easily add the term of reference and you would think that everything is done. For the most part this is going to solve most of your problems. However, you are going to start running into some issues where Drupal is expecting your title field. For instance, the content page (admin/content) is going to show all title empty because you never enter the title. Another example that you are going to run into is the title on your node page. At this point you might be confused and be like what should I do, I need to use the term of reference to accomplish my requirements but the title field is expected into few places. So they way to solve it is to set the same value from the term of reference into title field. The title field just been disable but not remove it from the actual content type. You can set the variable by using the rules module. You probably can use the code below to import the rule.

{
    "rules_title_taxonomy_token": {
		"LABEL": "title taxonomy token",
		"PLUGIN": "reaction rule",
		"OWNER": "rules",
		"REQUIRES": ["rules"],
		"ON": {
			"node_insert--package": {
				"bundle": "package"
			},
			"node_update--package": {
				"bundle": "package"
			}
		},
		"DO": [{
			"data_set": {
				"data": ["node:title"],
				"value": ["node:field-my-taxonomy-field:0:name"]
			}
		}]
	}
}

Be sure to change “package” to the name of your content type and be sure to use the proper field name for your taxonomy instead of field-my-taxonomy-field. Unfortunately, sometimes this import does not work because of some of customization that on the fields. If this is the case you would have to generate the rule by using the GUI.

First you need to set events two events:

  • After saving new content of type ContentTypeName
  • After updating existing content of type ContentTypeName

'Rules Set Event'

Then you should set an action called:

Set a data value [node:field-my-taxonomy-field:0:name] data should be [node:title]

'Rules Set Value'

From here you should be able to have term of reference as your title enjoy.

comments powered by Disqus