You are here

function media_browser_plus_enable in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7 media_browser_plus.install \media_browser_plus_enable()
  2. 7.2 media_browser_plus.install \media_browser_plus_enable()

Implements hook_enable().

Ensure the necessary structures exist.

File

./media_browser_plus.install, line 20
Install file for media_browser_plus.

Code

function media_browser_plus_enable() {

  // Folder vocabulary and root term.
  $root_folder = media_browser_plus_get_media_root_folder(TRUE);
  $vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');

  // Create the folder field.
  $field = array(
    'field_name' => 'field_folder',
    'label' => st('Media Folder'),
    'type' => 'taxonomy_term_reference',
    // Media file can only be in one folder at a time.
    'cardinality' => 1,
    'entity_type' => 'file',
    'bundle' => 'image',
    'required' => TRUE,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  $field_info = field_info_field($field['field_name']);
  if (empty($field_info)) {
    field_create_field($field);
  }

  // Backward compatibility handling.
  // @TODO Version compatibility cleanup.
  switch (TRUE) {

    // 23. April 2013 http://drupal.org/node/1977728
    case function_exists('file_type_load_all'):
      $file_types = array_keys(file_type_load_all());
      break;

    // Old.
    case function_exists('file_type_get_all_types'):
      $file_types = array_keys(file_type_get_all_types());
      break;

    // Very old.
    default:
      $file_types = array_keys(file_info_file_types());
  }

  // Ensure instance for each file bundle.
  foreach ($file_types as $bundle) {
    $field['bundle'] = $bundle;
    $instance_info = field_info_instance($field['entity_type'], $field['field_name'], $field['bundle']);
    if (empty($instance_info)) {
      field_create_instance($field);
    }
  }
}