summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNedjo Rogers2012-08-20 19:37:59 (GMT)
committer Nedjo Rogers2012-08-20 19:37:59 (GMT)
commit979755fbaa993fd836bd88b0a5022f3bb9a7cc88 (patch)
tree170274552efa54ec99f4707374c49809840fb1e1
parentb1a1412f74c1d8167a00c0f2012469d1233496f1 (diff)
Issue #1678240: enable bulk media upload for all fieldable entity types.
-rw-r--r--bulk_media_upload.admin.inc100
-rw-r--r--bulk_media_upload.info1
-rw-r--r--bulk_media_upload.summary.inc4
-rw-r--r--bulk_media_upload.upload.inc158
4 files changed, 172 insertions, 91 deletions
diff --git a/bulk_media_upload.admin.inc b/bulk_media_upload.admin.inc
index 9fa619b..71c5583 100644
--- a/bulk_media_upload.admin.inc
+++ b/bulk_media_upload.admin.inc
@@ -11,41 +11,85 @@
function bulk_media_upload_admin($form, &$form_state) {
$form = array();
- $nodetypes = array();
- foreach (node_type_get_types() as $type) {
- $nodetypes[$type->type] = $type->name;
+ $entity_types = entity_get_info();
+ $options = array();
+
+ foreach ($entity_types as $entity_type => $entity_info) {
+ if ($entity_info['fieldable'] && entity_type_supports($entity_type, 'save')) {
+ $types[$entity_type] = $entity_info['label'];
+ }
+ }
+
+ if (isset($form_state['values']['bulk_media_upload_entity_type'])) {
+ $selected_entity_type = $form_state['values']['bulk_media_upload_entity_type'];
+ }
+ else {
+ $selected_entity_type = variable_get('bulk_media_upload_entity_type');
}
- $form['bulk_media_upload_nodetype'] = array(
+ $form['bulk_media_upload_entity_type'] = array(
'#type' => 'select',
- '#title' => t('Nodetype'),
- '#default_value' => variable_get('bulk_media_upload_nodetype'),
- '#description' => t('Which Nodetype should be generated'),
+ '#title' => t('Entity type'),
+ '#default_value' => $selected_entity_type,
+ '#description' => t('Which entity type should be generated?'),
'#required' => TRUE,
- '#options' => $nodetypes,
+ '#options' => $types,
'#ajax' => array(
'callback' => 'bulk_media_upload_admin_ajax',
- 'wrapper' => 'mediaelement-div',
+ 'wrapper' => 'bundle-div',
'method' => 'replace',
'effect' => 'fade',
),
);
- $form['mediafield-wrapper'] = array(
- '#prefix' => '<div id="mediaelement-div">',
+ $form['bundle-wrapper'] = array(
+ '#prefix' => '<div id="bundle-div">',
'#suffix' => '</div>',
);
- if (isset($form_state['values']['bulk_media_upload_nodetype'])) {
- $nodetype = $form_state['values']['bulk_media_upload_nodetype'];
- }
- else {
- $nodetype = variable_get('bulk_media_upload_nodetype');
+ if ($selected_entity_type) {
+ $bundles = array();
+ foreach ($entity_types[$selected_entity_type]['bundles'] as $bundle_name => $bundle_info) {
+ $bundles[$bundle_name] = check_plain($bundle_info['label']);
+ }
+
+ if (isset($form_state['values']['bulk_media_upload_bundle'])) {
+ $selected_bundle = $form_state['values']['bulk_media_upload_bundle'];
+ }
+ else {
+ $selected_bundle = variable_get('bulk_media_upload_bundle');
+ }
+
+ // If we have stale data from a previous save, use the first available
+ // value.
+ if (!isset($bundles[$selected_bundle])) {
+ $selected_bundle = key(reset($bundles));
+ }
+
+ $form['bundle-wrapper']['bulk_media_upload_bundle'] = array(
+ '#type' => 'select',
+ '#title' => t('Bundle'),
+ '#default_value' => $selected_bundle,
+ '#description' => t('Which bundle should be generated?'),
+ '#required' => $selected_entity_type ? TRUE : FALSE,
+ '#options' => $bundles,
+ '#ajax' => array(
+ 'callback' => 'bulk_media_upload_admin_ajax',
+ 'wrapper' => 'mediaelement-div',
+ 'method' => 'replace',
+ 'effect' => 'fade',
+ ),
+ );
}
- if ($nodetype) {
+ $form['bundle-wrapper']['mediafield-wrapper'] = array(
+ '#prefix' => '<div id="mediaelement-div">',
+ '#suffix' => '</div>',
+ );
+
+ if ($selected_entity_type && $selected_bundle) {
// Search for supported fields and add them to the dropdown.
- $fields = field_info_instances('node', $nodetype);
+ $fields = field_info_instances($selected_entity_type, $selected_bundle);
$supported_modules = array('media', 'image', 'file');
$mediafields = array();
@@ -57,7 +101,7 @@ function bulk_media_upload_admin($form, &$form_state) {
}
if (count($mediafields) > 0) {
- $form['mediafield-wrapper']['bulk_media_upload_mediafield'] = array(
+ $form['bundle-wrapper']['mediafield-wrapper']['bulk_media_upload_mediafield'] = array(
'#type' => 'select',
'#title' => t('Mediafield'),
'#default_value' => variable_get('bulk_media_upload_mediafield'),
@@ -67,9 +111,8 @@ function bulk_media_upload_admin($form, &$form_state) {
);
}
else {
- $form['mediafield-wrapper']['bulk_media_upload_mediafield_info'] = array(
- '#markup' => '<p>There are no supported fields (' . implode(', ', $supported_modules) . ') assigned to nodetype <strong>' . $nodetype . '</strong></p>',
-
+ $form['bundle-wrapper']['mediafield-wrapper']['bulk_media_upload_mediafield_info'] = array(
+ '#markup' => '<p>' . t('There are no supported fields (@fields) assigned to bundle <strong>@bundle.</strong>', array('@fields' => implode(', ', $supported_modules), '@bundle' => $selected_bundle)) . '</p>',
);
}
}
@@ -82,8 +125,10 @@ function bulk_media_upload_admin($form, &$form_state) {
*/
function bulk_media_upload_admin_validate($form, &$form_state) {
if ($form_state['submitted']) {
- if (!isset($form_state['values']['bulk_media_upload_mediafield']) || $form_state['values']['bulk_media_upload_mediafield'] == '') {
- form_set_error('', t('You must select a mediafield'));
+ foreach (array('bulk_media_upload_bundle' => t('bundle'), 'bulk_media_upload_mediafield' => t('media field')) as $setting => $label) {
+ if (!isset($form_state['values'][$setting]) || $form_state['values'][$setting] == '') {
+ form_set_error('', t('You must select a !label.', array('!label' => $label)));
+ }
}
}
}
@@ -92,5 +137,10 @@ function bulk_media_upload_admin_validate($form, &$form_state) {
* Returns media field wrapper for ajax request
*/
function bulk_media_upload_admin_ajax($form, $form_state) {
- return $form['mediafield-wrapper'];
+ switch (end($form_state['triggering_element']['#parents'])) {
+ case 'bulk_media_upload_entity_type':
+ return $form['bundle-wrapper'];
+ case 'bulk_media_upload_bundle':
+ return $form['bundle-wrapper']['mediafield-wrapper'];
+ }
}
diff --git a/bulk_media_upload.info b/bulk_media_upload.info
index a220a3f..33c5e8b 100644
--- a/bulk_media_upload.info
+++ b/bulk_media_upload.info
@@ -3,5 +3,6 @@ core = 7.x
package = Media
description = With Bulk Media Upload you can upload a bunch of media files and a new node will be created for every file.
+dependencies[] = entity
dependencies[] = plupload
dependencies[] = token
diff --git a/bulk_media_upload.summary.inc b/bulk_media_upload.summary.inc
index 1d35441..d76195f 100644
--- a/bulk_media_upload.summary.inc
+++ b/bulk_media_upload.summary.inc
@@ -12,7 +12,7 @@ function bulk_media_upload_summary($form, $form_state) {
module_load_include('inc', 'node', 'node.admin');
$form = array();
- if (!isset($_SESSION['bulk_media_upload_batch_result']['nids'])) {
+ if (!isset($_SESSION['bulk_media_upload_batch_result']['ids'])) {
$form['info'] = array(
'#markup' => '<p>' . t('There was no media uploaded in this session.') . '</p>',
);
@@ -73,7 +73,7 @@ function bulk_media_upload_summary($form, $form_state) {
$nids = $query
->fields('n', array('nid'))
- ->condition('n.nid', $_SESSION['bulk_media_upload_batch_result']['nids'], 'IN')
+ ->condition('n.nid', $_SESSION['bulk_media_upload_batch_result']['ids'], 'IN')
->limit(50)
->orderByHeader($header)
->execute()
diff --git a/bulk_media_upload.upload.inc b/bulk_media_upload.upload.inc
index 2cf0a89..8e8d418 100644
--- a/bulk_media_upload.upload.inc
+++ b/bulk_media_upload.upload.inc
@@ -1,7 +1,7 @@
<?php
/**
* @file
- * Upload form and node generation functions
+ * Upload form and entity generation functions.
*
*/
@@ -11,19 +11,20 @@
function bulk_media_upload_upload_form($form, &$form_state) {
global $language;
- $nodetype = variable_get('bulk_media_upload_nodetype');
+ $entity_type = variable_get('bulk_media_upload_entity_type');
+ $entity_info = entity_get_info($entity_type);
+ $bundle = variable_get('bulk_media_upload_bundle');
$mediafield = variable_get('bulk_media_upload_mediafield');
- if (empty($nodetype) || empty($mediafield)) {
+ if (empty($entity_type) || empty($bundle) || empty($mediafield)) {
drupal_set_message(t('You have not configured the Bulk Media Upload module. Go to the ' . l(t('configuration page'), 'admin/config/media/bulk_media_upload') . ' and fix this!'), 'error');
return $form;
}
- $nodetype_info = node_type_load($nodetype);
- $mediafield_info = field_info_instance('node', $mediafield, $nodetype);
+ $mediafield_info = field_info_instance($entity_type, $mediafield, $bundle);
$form['import_information'] = array(
- '#markup' => '<p>' . t('Nodetype used for generating the nodes:') . ' <strong>' . $nodetype_info->name . ' (' . $nodetype_info->type . ')</strong></p>'
+ '#markup' => '<p>' . t('Bundle used for generating the entities:') . ' <strong>' . $entity_info['bundles'][$bundle]['label'] . ' (' . $bundle . ')</strong></p>'
. '<p>' . t('Mediafield used for uploading your media:') . ' <strong>' . $mediafield_info['label'] . ' (' . $mediafield_info['field_name'] . ')</strong></p>',
);
@@ -45,7 +46,7 @@ function bulk_media_upload_upload_form($form, &$form_state) {
$form['token_help']['help'] = array(
'#theme' => 'token_tree',
- '#token_types' => array('node', 'file'),
+ '#token_types' => array($entity_type, 'file'),
'#global_types' => TRUE,
'#click_insert' => TRUE,
);
@@ -61,30 +62,29 @@ function bulk_media_upload_upload_form($form, &$form_state) {
$form['upload']['#upload_validators']['file_validate_extensions'][] = $mediafield_info['settings']['file_extensions'];
}
- $form['default_values'] = array(
- '#type' => 'fieldset',
- '#title' => t('Default Values'),
- '#tree' => TRUE,
- );
+ // Use the entity creation form to extract all assigned fields.
+ $allowed_fields = array_diff_key(field_info_instances($entity_type, $bundle), array($mediafield => NULL));
- // Use the node creation form to extract all assigned fields.
- $allowed_fields = array_diff_key(field_info_instances('node', $nodetype), array($mediafield => NULL));
+ // Create the new entity.
+ $entity = entity_create($entity_type, array(
+ $entity_info['entity keys']['bundle'] => $bundle,
+ ));
- $node = (object) array(
- 'uid' => 1,
- 'name' => '',
- 'type' => $nodetype,
- 'language' => LANGUAGE_NONE,
- );
- $nodeform = array();
+ $entity_form = array();
- // And add these fields to the default value fieldset without the media field.
- field_attach_form('node', $node, $nodeform, $form_state);
- $form['default_values'] = array_merge($form['default_values'], array_intersect_key($nodeform, $allowed_fields));
+ // And add these fields, if any, to a default value fieldset without the media field.
+ field_attach_form($entity_type, $entity, $entity_form, $form_state);
+ if ($additions = array_intersect_key($entity_form, $allowed_fields)) {
+ $form['default_values'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Default Values'),
+ '#tree' => TRUE,
+ ) + $additions;
+ }
$form['submit'] = array(
'#type' => 'submit',
- '#value' => t('Generate Nodes'),
+ '#value' => t('Generate @entity_type entities', array('@entity_type' => $entity_info['label'])),
);
return $form;
@@ -100,49 +100,56 @@ function bulk_media_upload_upload_form_validate($form, &$form_state) {
* Implements hook_submit().
*/
function bulk_media_upload_upload_form_submit($form, &$form_state) {
-
+ $entity_type = variable_get('bulk_media_upload_entity_type');
+ $entity_info = entity_get_info($entity_type);
+
// Create new taxonomy terms
- $fields = field_info_instances('node', variable_get('bulk_media_upload_nodetype'));
- foreach ($fields as $field) {
- $fieldinfo = field_info_field($field['field_name']);
- if($fieldinfo['module'] == 'taxonomy'){
- foreach ($form_state['values']['default_values'][$field['field_name']][LANGUAGE_NONE] as $key => $term) {
- if($term['tid'] == 'autocreate'){
- // Create term.
- $term = (object) $term;
- unset($term->tid);
- taxonomy_term_save($term);
- $form_state['values']['default_values'][$field['field_name']][LANGUAGE_NONE][$key] = (array) $term;
+ if (isset($form['default_values'])) {
+ $fields = field_info_instances(variable_get('bulk_media_upload_entity_type'), variable_get('bulk_media_upload_bundle'));
+ foreach ($fields as $field) {
+ $fieldinfo = field_info_field($field['field_name']);
+ if($fieldinfo['module'] == 'taxonomy'){
+ foreach ($form_state['values']['default_values'][$field['field_name']][LANGUAGE_NONE] as $key => $term) {
+ if($term['tid'] == 'autocreate'){
+ // Create term.
+ $term = (object) $term;
+ unset($term->tid);
+ taxonomy_term_save($term);
+ $form_state['values']['default_values'][$field['field_name']][LANGUAGE_NONE][$key] = (array) $term;
+ }
}
}
-
}
}
// Prepare the batch process.
+ $placeholders = array('@entity_type' => $entity_info['label']);
$batch = array(
- 'title' => t('Generating Nodes'),
- 'error_message' => t('Node generation has encountered an error.'),
+ 'title' => t('Generating @entity_type entities', $placeholders),
+ 'error_message' => t('@entity_type generation has encountered an error.', $placeholders),
'finished' => '_bulk_media_upload_batch_finished',
'file' => drupal_get_path('module', 'bulk_media_upload') . '/bulk_media_upload.upload.inc',
);
foreach ($form_state['values']['upload'] as $tmpfile) {
- $batch['operations'][] = array('_bulk_media_upload_generate_node', array($tmpfile, $form, $form_state));
+ $batch['operations'][] = array('_bulk_media_upload_generate_entity', array($tmpfile, $form, $form_state));
}
batch_set($batch);
}
/**
- * Internal function for node generation.
+ * Internal function for entity generation.
*/
-function _bulk_media_upload_generate_node($tmpfile, $form, $form_state, &$context) {
+function _bulk_media_upload_generate_entity($tmpfile, $form, $form_state, &$context) {
global $user;
-
+ $entity_type = variable_get('bulk_media_upload_entity_type');
+ $entity_info = entity_get_info($entity_type);
+ // Default label field to 'name'.
+ $label_field = isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : 'name';
$mediafield_name = variable_get('bulk_media_upload_mediafield');
- $nodetype = variable_get('bulk_media_upload_nodetype');
- $mediafield_info = field_info_instance('node', $mediafield_name, $nodetype);
+ $bundle = variable_get('bulk_media_upload_bundle');
+ $mediafield_info = field_info_instance($entity_type, $mediafield_name, $bundle);
// Save media file.
$scheme = variable_get('file_default_scheme', 'public') . '://';
@@ -179,15 +186,29 @@ function _bulk_media_upload_generate_node($tmpfile, $form, $form_state, &$contex
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
- // Save the node.
- $node = new stdClass();
- $node->type = variable_get('bulk_media_upload_nodetype');
- node_object_prepare($node);
-
- $node->title = $file->filename;
- $node->language = LANGUAGE_NONE;
-
- node_save($node);
+ // Create the new entity.
+ $entity = entity_create($entity_type, array(
+ $entity_info['entity keys']['bundle'] => $bundle,
+ $label_field => $file->filename,
+ ));
+
+ // Taxonomy terms need to have their vocabulary ID added.
+ // See http://drupal.org/node/1409256.
+ if ($entity_type == 'taxonomy_term') {
+ if ($vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name)) {
+ $entity->vid = $vocabulary->vid;
+ }
+ }
+ // If the entity has a property tied to UID, set it so that, e.g., nodes will
+ // be assigned an author.
+ $info = entity_get_property_info($entity_type);
+ foreach ($info['properties'] as $property) {
+ if (isset($property['schema field']) && $property['schema field'] == 'uid') {
+ $entity->uid = $user->uid;
+ break;
+ }
+ }
+ entity_save($entity_type, $entity);
// Create the media field.
$fieldinfo = field_info_field($mediafield_name);
@@ -202,27 +223,36 @@ function _bulk_media_upload_generate_node($tmpfile, $form, $form_state, &$contex
$settings['description'] = '';
};
- $node->$mediafield_name = array(LANGUAGE_NONE => array(0 => $settings));
+ $entity->$mediafield_name = array(LANGUAGE_NONE => array(0 => $settings));
// Default values.
- field_attach_submit('node', $node, $form['default_values'], $form_state);
+ if (isset($form['default_values'])) {
+ field_attach_submit($entity_type, $entity, $form['default_values'], $form_state);
+ }
+
+ entity_save($entity_type, $entity);
- node_save($node);
+ // Replace tokens in title - this has to be done after entity_save.
+ $label = token_replace($form_state['values']['title'], array($entity_type => $entity, 'file' => $file));
- // Replace tokens in title - this have to be done after node_save.
- $node->title = token_replace($form_state['values']['title'], array('node' => $node, 'file' => $file));
+ $entity->$label_field = $label;
- node_save($node);
+ entity_save($entity_type, $entity);
$context['message'] = t('Importing: @filename', array('@filename' => $file->filename));
- $context['results']['nids'][] = $node->nid;
+ $context['results']['ids'][] = $entity->$entity_info['entity keys']['id'];
}
/**
* Finished function for batch
*/
function _bulk_media_upload_batch_finished($success, $results, $operations) {
- drupal_set_message(t('Node generation completed'));
+ $entity_type = variable_get('bulk_media_upload_entity_type');
+ $entity_info = entity_get_info($entity_type);
+ drupal_set_message(t('@entity_type generation completed.', array('@entity_type' => $entity_info['label'])));
$_SESSION['bulk_media_upload_batch_result'] = $results;
- drupal_goto('admin/content/media/bulk_upload/summary');
+ // TODO: do landing pages for other entity types?
+ if ($entity_type == 'node') {
+ drupal_goto('admin/content/media/bulk_upload/summary');
+ }
}