This is the last post in the Easy Integration of Rich Internet Applications with Drupal series. It describes the Drupal installation hooks that automate the creation of the Rich Internet Application content type as well as that of its related fields..
- /**
- *
- * Installs a new content type ria and attaches custom fields to it.
- */
- function ria_install() {
- $t = get_t();
- 'type' => 'ria',
- 'name' => $t('RIA'),
- 'description' => $t('A Rich Internet Application'),
- 'base' => 'node_content',
- 'body_label' => 'HTML'
- );
- $node_type_ria = node_type_set_defaults($node_type_ria_attributes);
- node_add_body_field($node_type_ria);
- node_type_save($node_type_ria);
- _ria_install_fields();
- }
Creating RIA content type fields and field instances.
- function _ria_install_fields() {
- $t = get_t();
- // Field types
- 'field_name' => 'field_ria_application_directory',
- 'type' => 'text'
- );
- 'field_name' => 'field_ria_javascript_api',
- 'type' => 'text',
- 'cardinality' => 3
- );
- 'field_name' => 'field_ria_css',
- 'type' => 'text'
- );
- 'field_name' => 'field_ria_application_javascript',
- 'type' => 'text'
- );
- 'field_name' => 'field_ria_inline_javascript',
- 'type' => 'text'
- );
- 'field_name' => 'field_ria_description',
- 'type' => 'text_with_summary'
- );
- foreach ($field_types as $name => $field_type) {
- field_create_field($field_type);
- }
- // Display
- 'label' => 'hidden',
- 'type' => 'hidden'
- ),
- 'label' => 'hidden',
- 'type' => 'hidden'
- )
- );
- // Field instances
- 'field_name' => 'field_ria_application_directory',
- 'label' => $t('Application Directory'),
- 'description' => $t('Directory where application resources will be stored.'),
- 'required' => TRUE,
- 'display' => $display_hidden
- );
- 'field_name' => 'field_ria_javascript_api',
- 'label' => $t('JavaScript API'),
- 'description' => $t('URL of a JavaScript library used by the application.'),
- 'display' => $display_hidden
- );
- 'field_name' => 'field_ria_application_javascript',
- 'label' => $t('Application JavaScript'),
- 'description' => $t('Javascript file name.'),
- 'required' => TRUE,
- 'display' => $display_hidden
- );
- 'field_name' => 'field_ria_inline_javascript',
- 'label' => $t('Inline Javascript'),
- 'description' => $t("Any Javascript that needs to be added to document's head."),
- 'display' => $display_hidden
- );
- 'field_name' => 'field_ria_css',
- 'label' => $t('CSS'),
- 'description' => $t('Custom CSS file name.'),
- 'display' => $display_hidden
- );
- 'field_name' => 'field_ria_description',
- 'label' => $t('Description'),
- 'description' => $t('Description of what the application does.'),
- 'required' => TRUE,
- )
- );
- foreach ($field_instances as $name => $instance) {
- $instance['entity_type'] = 'node';
- $instance['bundle'] = 'ria';
- field_create_instance($instance);
- }
- // Change settings for field body
- $body = field_read_instance('node', 'body', 'ria');
- 'entity_type' => 'node',
- 'field_name' => 'body',
- 'bundle' => 'ria',
- 'label' => $t('HTML'),
- )
- );
- field_update_instance($body_instance);
- }
<--Uninstall hook --> The uninstall hook.
- function ria_uninstall() {
- node_type_delete('ria');
- _ria_delete_fields();
- }
- function _ria_delete_fields() {
- $fields = array('field_ria_application_directory', 'field_ria_javascript_api', 'field_ria_application_javascript',
- 'field_ria_inline_javascript', 'field_ria_description', 'field_ria_css');
- foreach ($fields as $field) {
- field_delete_field($field);
- }
- }