media_browser_plus.install in Media Browser Plus 7.2
Same filename and directory in other branches
Install file for media_browser_plus.
File
media_browser_plus.installView source
<?php
/**
* @file
* Install file for media_browser_plus.
*/
/**
* Implements hook_install().
*/
function media_browser_plus_install() {
// Make sure the standard 'field_tags' field exists.
_media_browser_plus_ensure_field_folder();
// On installation attempt we have to trust the install profile - otherwise
// we get collisions with the tag name.
if (drupal_installation_attempted() && !variable_get('simpletest_parent_profile', FALSE)) {
_media_browser_plus_ensure_field_tags();
}
}
/**
* 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);
// Latest file entity version.
if (function_exists('file_type_get_all_types')) {
$file_types = array_keys(file_type_get_all_types());
}
elseif (function_exists('file_type_load_all')) {
$file_types = array_keys(file_type_load_all());
}
else {
$file_types = array_keys(file_info_file_types());
}
// Ensure instance for each media bundle.
foreach ($file_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);
// Latest file entity version.
if (function_exists('file_type_get_all_types')) {
$file_types = array_keys(file_type_get_all_types());
}
elseif (function_exists('file_type_load_all')) {
$file_types = array_keys(file_type_load_all());
}
else {
$file_types = array_keys(file_info_file_types());
}
// Ensure instance for each media bundle.
foreach ($file_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 array $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);
}
}
/**
* Creates an instance if this one doesn't already exist.
*
* @param array $instance
* The field 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);
variable_set('media_browser_plus_thumbnails_as_default_browser', TRUE);
// Create the media folder.
media_browser_plus_get_media_root_folder(TRUE);
$dir = variable_get('file_default_scheme', 'public') . '://' . variable_get('media_root_folder') . '/';
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
}
/**
* Implements hook_uninstall().
*/
function media_browser_plus_uninstall() {
variable_del('media_media_per_page');
variable_del('media_grid_window_height');
variable_del('media_page_items_per_page');
variable_del('media_root_folder');
variable_del('media_browser_plus_thumbnails_as_default_browser');
}
/**
* Ensure the media_browser_plus fields are attached on all file types.
*/
function media_browser_plus_update_7001() {
_media_browser_plus_ensure_field_folder();
_media_browser_plus_ensure_field_tags();
}
Functions
Name![]() |
Description |
---|---|
media_browser_plus_enable | Implements hook_enable(). |
media_browser_plus_install | Implements hook_install(). |
media_browser_plus_uninstall | Implements hook_uninstall(). |
media_browser_plus_update_7001 | Ensure the media_browser_plus fields are attached on all file types. |
_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 | Creates an instance if this one doesn't already exist. |