You are here

public static function ScaldAtomController::addType in Scald: Media Management made easy 7

Add a Scald unified type.

This function create a new type if it does not already exist. It can be used inside atom providers hook_install(), to easily ensure that the type of the atom the module will provide is defined.

Return value

bool TRUE if the new type was added, FALSE if already exists.

5 calls to ScaldAtomController::addType()
scald_add_type in ./scald.module
Add a Scald unified type.
scald_audio_install in modules/providers/scald_audio/scald_audio.install
Implements hook_install().
scald_flash_install in modules/providers/scald_flash/scald_flash.install
Implements hook_install().
scald_image_install in modules/providers/scald_image/scald_image.install
Implements hook_install().
scald_video_install in modules/providers/scald_video/scald_video.install
Implements hook_install().

File

includes/ScaldAtomController.inc, line 81
This file contains the Scald Atom controller.

Class

ScaldAtomController
Controller class for Scald Atoms.

Code

public static function addType($type, $title, $description) {

  // Check if this type already exists.
  $types = scald_types();
  if (!empty($types[$type])) {
    return FALSE;
  }

  // Create a new type.
  db_insert('scald_types')
    ->fields(array(
    'type',
    'title',
    'description',
    'provider',
  ))
    ->values(array(
    $type,
    $title,
    $description,
    'scald',
  ))
    ->execute();

  // And add fields on it, starting with the Scald Thumbnail field.
  $instance = array(
    'field_name' => 'scald_thumbnail',
    'entity_type' => 'scald_atom',
    'bundle' => $type,
    'label' => 'Thumbnail',
    'required' => FALSE,
    'display' => array(
      'default' => array(
        'type' => 'hidden',
      ),
    ),
    'settings' => array(
      'file_directory' => 'thumbnails/' . $type,
    ),
  );
  if (!field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'])) {
    field_create_instance($instance);
    $instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
    foreach ($instance['display'] as $view_mode => $settings) {
      $instance['display'][$view_mode]['type'] = 'hidden';
    }
    field_update_instance($instance);
  }

  // Instantiate the Scald Authors field, if the vocabulary exists, and if
  // the field exists for us to instantiate. Otherwise, assume that one or
  // both were intentionally deleted and don't re-create.
  $vocabulary_name = variable_get('scald_author_vocabulary', 'scald_authors');
  $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
  $instance = array(
    'field_name' => 'scald_authors',
    'entity_type' => 'scald_atom',
    'bundle' => $type,
    'label' => 'Authors',
    'required' => FALSE,
    'widget' => array(
      'type' => 'taxonomy_autocomplete',
    ),
  );
  if ($vocabulary && field_read_field($instance['field_name']) && !field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'])) {
    field_create_instance($instance);
  }

  // Instantiate the Scald Tags field. As with Scald Authors above, only do
  // this if the vocabulary and the field already exist.
  $vocabulary_name = variable_get('scald_tags_vocabulary', 'scald_tags');
  $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
  $instance = array(
    'field_name' => 'scald_tags',
    'entity_type' => 'scald_atom',
    'bundle' => $type,
    'label' => 'Tags',
    'required' => FALSE,
    'widget' => array(
      'type' => 'taxonomy_autocomplete',
    ),
  );
  if ($vocabulary && field_read_field($instance['field_name']) && !field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'])) {
    field_create_instance($instance);
  }

  // Flush our caches.
  scald_contexts(TRUE);
  scald_types(TRUE);
  return TRUE;
}