View source
<?php
define('EXCLUDE_FROM_LIBRARY', 0);
define('INCLUDE_IN_LIBRARY', 1);
function media_library_permission() {
return array(
'media library add to library' => array(
'title' => t('Add files to the Media Library'),
),
);
}
function media_library_menu() {
$items['admin/config/media/media-library'] = array(
'title' => 'Media library',
'description' => 'Show or hide library settings and configure default opt-in/opt-out behavior.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'media_library_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'media_library.admin.inc',
);
return $items;
}
function media_library_menu_alter(&$items) {
$items['admin/content/file/library'] = array(
'title' => 'Media Library',
'description' => 'Manage files in the media library.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'file_entity_admin_file',
),
'access arguments' => array(
'administer files',
),
'type' => MENU_LOCAL_TASK,
'file' => 'file_entity.admin.inc',
'file path' => drupal_get_path('module', 'file_entity'),
'weight' => 20,
);
}
function media_library_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/content/file/library') {
$item = menu_get_item('file/add');
if (!empty($item['access'])) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
'#weight' => $item['weight'],
);
}
}
}
function media_library_field_info_alter(&$info) {
foreach ($info as $field_type => &$field_type_info) {
if ($field_type == 'file' || $field_type == 'image') {
$field_type_info += array(
'instance_settings' => array(),
);
$field_type_info['instance_settings'] += array(
'media_library_include_in_library' => FALSE,
);
}
}
}
function media_library_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$instance = $form['#instance'];
$field_type = $form['#field']['type'];
$allowed_field_types = array(
'file',
'image',
);
if (in_array($field_type, $allowed_field_types)) {
$form['instance']['settings']['media_library_include_in_library'] = array(
'#type' => 'checkbox',
'#title' => t('Include in media library'),
'#default_value' => $instance['settings']['media_library_include_in_library'],
'#weight' => 50,
);
}
}
function media_library_field_attach_presave($entity_type, $entity) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
$field = field_info_field($instance['field_name']);
$instance = field_info_instance($entity_type, $field_name, $bundle);
if ($field['type'] == 'file' || $field['type'] == 'image') {
if (!empty($entity->{$field_name})) {
foreach ($entity->{$field_name} as $langcode => $items) {
foreach ($items as $delta => $item) {
if (isset($instance['settings']['media_library_include_in_library'])) {
$file = file_load($item['fid']);
$file->library = $instance['settings']['media_library_include_in_library'];
file_save($file);
}
}
}
}
}
}
}
function media_library_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) {
if (!empty($form['#step']) && 4 === $form['#step']) {
$form['library'] = array(
'#type' => 'checkbox',
'#access' => user_access('media library add to library'),
'#title' => t('Include in library'),
'#default_value' => variable_get('media_library_file_add_default', FALSE),
'#weight' => 50,
);
}
}
function media_library_form_file_entity_edit_alter(&$form, &$form_state, $form_id) {
$file = $form_state['file'];
$form['library_settings'] = array(
'#type' => 'fieldset',
'#access' => user_access('media library add to library'),
'#title' => t('Library settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'file-form-library',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'media_library') . '/media_library.js',
),
),
'#weight' => 90,
);
$form['library_settings']['library'] = array(
'#type' => 'checkbox',
'#title' => t('Include in library'),
'#default_value' => $file->library,
'#weight' => 50,
);
}
function media_library_query_alter($query) {
if (!module_exists('admin_views')) {
if (current_path() == 'admin/content/file/library') {
$tables = $query
->getTables();
if (!empty($tables['base']['table']) && $tables['base']['table'] == 'file_managed') {
$query
->condition('library', INCLUDE_IN_LIBRARY, '=');
}
}
}
}
function media_library_views_api() {
return array(
'api' => 3,
);
}