Content type
is both a technical term used in Drupal and the name of my module for creating content types in Drupal. Content type can help make your module development faster.
When do you use Content type?
Use Content type when you create a module needing a basic content type. Use Content type when you install a module with Content type listed as a prerequisite.
When do you not use Content type?
The current D7 version does not define fields. You could define the base type using Content type then add fields from your code. The field handling could be added because the code exists in related modules.
If you have a need and want to contribute ideas, code, or money, the Content type module could be expanded to handle 99.9% of cases instead of 90%. 100% coverage is unlikely because people are continually adding new features to the fields system.
How do you set up a content type?
We will use the example module named Alarm clock. Alarm clock has a file named alarm_clock.info. The Alarm clock module creates a content type named Alarm clock with an internal name of alarm_clock.
- Create a text file named
alarm_clock.content_type.txt. - Add the following to alarm_clock.info:
content_type[] = alarm_clock.content_type.txt - Add your content type definition to
alarm_clock.content_type.txt.
alarm_clock.content_type.txt can have any name you want so long as the name is usable in the .info file. Add quotes around the file name in .info if the file name contains spaces.
alarm_clock.content_type.txt can start with lists of other files as shown next. You can create one file per content type or include multiple content types per file. Any of the included files can contain references to other files. The only requirement is to place all the statements for a content type in the same file. The files are read one at a time and all the content types in a file are created before moving on to the next file.content_type[] = abc.textcontent_type[] = efg.txt
The following example content type is from an early version of the Alarm clock module. Note the internal name, or machine name, of the content type is used as the first level identifier for all the lines defining the content type. Lines starting with ; are comments. Lines found in a normal module .info file are ignored.;name = Alarm clock
description = Alarm clock for your Web site processes.
; From a recipe invented before electricity.
;version = "7.x-1"
; See http://ka.tl for more details.
alarm_clock[name] = Alarm clock
alarm_clock[description] = Alarm clock for your Web site processes.
alarm_clock[title_label] = Alarm
alarm_clock[body_label] = Description
alarm_clock[published] = true
alarm_clock[preview] = optional
The .info format
The following comments are in the Drupal definition of their .info file and and is similar to a PHP.ini file. The examples show you the range of options for writing an entry.key = valuekey = "value"key = 'value'key = "multi-line
value"key = 'multi-line
value'key
=
'value'key[] = "numeric array"key[index] = "associative array"key[index][] = "nested numeric array"key[index][index] = "nested associative array"
The content type definition
Your text files can contain the following definitions. They link to the formal definitions.alarm_clock[name] = Example content typealarm_clock[description] = jkhjkhdkjhsdkfhdfkfkjdfhkhkhsdsjkalarm_clock[description][] = jkhjkhdkjhsdkfhdfkfkjdfhkhkhsdsjkalarm_clock[title_label] = Example titlealarm_clock[preview] = disabledalarm_clock[preview] = optionalalarm_clock[preview] = requiredalarm_clock[submission_guidelines][] = kljgklgjlkjsdflksdfl;alarm_clock[published] = truealarm_clock[published] = falsealarm_clock[promoted] = truealarm_clock[promoted] = falsealarm_clock[sticky] = truealarm_clock[sticky] = falsealarm_clock[revisions] = truealarm_clock[revisions] = falsealarm_clock[author_and_date] = truealarm_clock[author_and_date] = false
Name
The first part, alarm_clock, is the key that links all the definition items for one content type and is the internal name or machine name of the content type you are defining. The name part tell the Content type module to use the rest of the line for the Name when defining a content type. Keep this name short and put any long text in the description field. The Drupal content type definition screen has the following description of the field.
The human-readable name of this content type. This text will be displayed as part of the list on the Add new content page. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.
Description
alarm_clock[description] = jkhjkhdkjhsdkfhdfkfkjdfhkhkhsdsjkalarm_clock[description][] = jkhjkhdkjhsdkfhdfkfkjdfhkhkhsdsjk
Describe this content type. The text will be displayed on the Add new content page.
You can provide a single line description or a multiline description. The multiline format can be any of the following.alarm_clock[description] = 'multi-line
value'
alarm_clock[description][] = 'multi-line
value 1'
alarm_clock[description][] = 'multi-line
value 2 will be added to value 1'
Title label
This line defines the Title field label for your content type title. In the Alarm clock module, the title is named Alarm.
Preview
This is the Preview before submitting field and may have one of the three following values. the default in Drupal is optional.alarm_clock[preview] = disabledalarm_clock[preview] = optionalalarm_clock[preview] = required
Submission guidelines
From Drupal: Explanation or submission guidelines This text will be displayed at the top of the page when creating or editing content of this type.
This setting has the same multiline options as description.
Published
alarm_clock[published] = truealarm_clock[published] = false
This is the Drupal option labelled: Publishing options [ ] Published
You can set the default for the content type to true or false or not bother setting it.
Promoted
alarm_clock[promoted] = truealarm_clock[promoted] = false
This is the Drupal option labelled: Publishing options [ ] Promoted to front page
You can set the default for the content type to true or false or leave out this setting.
Revisions
alarm_clock[revisions] = truealarm_clock[revisions] = false
This is the Drupal option labelled: Publishing options [ ] Create new revision
You can set the default for the content type to on or off.
Author and date
alarm_clock[author_and_date] = truealarm_clock[author_and_date] = false
This is the Drupal option labelled: Display settings [ ] Display author and date information. Author username and publish date will be displayed.
Future development
Most of the ideas here are already implemented in some form in some of my other code.
I proposed this module as an enhancement for Drupal 8. The D8 version would include a way to set site wide defaults. I wrote something similar for an earlier Drupal and did not carry it over to D7. Site wide defaults should be set before the installation profile system kicks in so the profile can vary the defaults by reading and implementing more content type settings.
Module level defaults are possible and are in other versions of this module. I just have to choose a consistent way to introduce them. In one place I use the special content type name default to set the defaults. In another I use *. * fits the wildcard idea better and does not create a restriction on the naming of content types. I have not tested * through the Drupal .info file processing.









- Facebook Like
- Google Plus One
- Log in or register to post comments