You are here

media_browser_plus.install in Media Browser Plus 7

Same filename and directory in other branches
  1. 7.3 media_browser_plus.install
  2. 7.2 media_browser_plus.install

Install file for media_browser_plus.

File

media_browser_plus.install
View source
<?php

/**
 * @file
 * Install file for media_browser_plus.
 */

/**
 * Implements hook_install().
 */
function media_browser_plus_install() {
  variable_set('media_root_folder', 'media');

  // Make sure the standard 'field_tags' field exists.
  _media_browser_plus_ensure_field_tags();
  _media_browser_plus_ensure_field_folder();
}

/**
 * Make sure the field_tags field exists and is of the right type.
 */
function _media_browser_plus_ensure_field_tags() {
  $t = get_t();

  // Make sure the 'tags' vocabulary exists.
  $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
  if (!$vocabulary) {
    $description = st('Use tags to group articles on similar topics into categories.');
    $help = st('Enter a comma-separated list of words to describe your content.');
    $vocabulary = (object) array(
      'name' => 'Tags',
      'description' => $description,
      'machine_name' => 'tags',
      'help' => $help,
    );
    taxonomy_vocabulary_save($vocabulary);
  }
  $field = array(
    'field_name' => 'field_tags',
    'type' => 'taxonomy_term_reference',
    // Set cardinality to unlimited for tagging.
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'entity_type' => 'file',
    'label' => 'Tags',
    'widget' => array(
      'type' => 'taxonomy_autocomplete',
    ),
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  _media_browser_plus_ensure_field($field);

  // Ensure instance for each media bundle.
  foreach (array_keys(media_type_get_types()) as $bundle) {
    $field['bundle'] = $bundle;
    _media_browser_plus_ensure_instance($field);
  }
}

/**
 * Make sure the field_tags field exists and is of the right type.
 */
function _media_browser_plus_ensure_field_folder() {

  // Make sure the 'tags' vocabulary exists.
  $vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');
  if (!$vocabulary) {
    $description = st('Use media folders to organize your media');
    $help = st('Enter a concise name for the media folder');
    $vocabulary = (object) array(
      'name' => 'Media Folders',
      'description' => $description,
      'machine_name' => 'media_folders',
      'hierarchy' => 1,
      'help' => $help,
    );
    taxonomy_vocabulary_save($vocabulary);
  }
  $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,
        ),
      ),
    ),
  );
  _media_browser_plus_ensure_field($field);

  // Ensure instance for each media bundle.
  foreach (array_keys(media_type_get_types()) as $bundle) {
    $field['bundle'] = $bundle;
    _media_browser_plus_ensure_instance($field);
  }
}

/**
 * Create a field, unless it exists already.
 *
 * Note that it's not necessary to check field type here, as that's done in the
 * requirements step.
 *
 * @param $field
 *   The field definition.
 */
function _media_browser_plus_ensure_field($field) {
  $existing_field = field_info_field($field['field_name']);
  if (empty($existing_field)) {
    field_create_field($field);
  }
}

/**
 * @todo Document what this function does.
 *
 * @param $instance
 */
function _media_browser_plus_ensure_instance($instance) {
  $existing_instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  if (empty($existing_instance)) {
    field_create_instance($instance);
  }
}

/**
 * Implements hook_enable().
 */
function media_browser_plus_enable() {

  // Save default settings.
  variable_set('media_media_per_page', 30);
  variable_set('media_grid_window_height', 400);
  variable_set('media_page_items_per_page', 10);

  // Create the media folder.
  $dir = variable_get('file_default_scheme', 'public') . '://' . variable_get('media_root_folder') . '/';
  file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  $term = media_browser_plus_get_media_root_folder(TRUE);
  if (is_object($term)) {

    // Load all media and apply tid of folder if none set.
    variable_set('media_folder_import_start', 0);
    $batch = array(
      'title' => t('Assigning Media to Root Folder'),
      'operations' => array(
        array(
          'media_browser_plus_folder_media_import',
          array(
            $term->tid,
            $dir,
          ),
        ),
      ),
      'finished' => 'media_browser_plus_folder_media_import_finished',
      'file' => drupal_get_path('module', 'media_browser_plus') . '/includes/media_browser_plus.admin.inc',
    );
    batch_set($batch);
  }
}

/**
 * Implements hook_install().
 *
 * @TODO: discuss if this should be done
 */
function media_browser_plus_uninstall() {
  field_delete_field('field_folder');
  field_delete_field('field_tags');
  variable_del('media_media_per_page');
  variable_del('media_grid_window_height');
  variable_del('media_page_items_per_page');
  variable_del('media_root_folder');
}

Functions

Namesort descending Description
media_browser_plus_enable Implements hook_enable().
media_browser_plus_install Implements hook_install().
media_browser_plus_uninstall Implements hook_install().
_media_browser_plus_ensure_field Create a field, unless it exists already.
_media_browser_plus_ensure_field_folder Make sure the field_tags field exists and is of the right type.
_media_browser_plus_ensure_field_tags Make sure the field_tags field exists and is of the right type.
_media_browser_plus_ensure_instance @todo Document what this function does.