FINAL suepr merge step : added all modules to this super repos

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 16:46:59 +02:00
7585 changed files with 1723356 additions and 18 deletions

View File

@@ -0,0 +1,214 @@
<?php
/**
* @file
* Features module integration.
*/
/**
* Implements hook_features_api().
*
* Main info hook that features uses to determine what components are provided
*
* The 'component' for this module is named 'filefield_paths' not just
* 'filefield_paths' to follow recommended practice documented in features.api
*
* We export individual filefield_paths instances, although seldom on their
* own, usually as part of a bigger package. When a content type or field is
* being exported, these settings come along for the ride.
*/
function filefield_paths_features_api() {
return array(
'filefield_paths' => array(
'name' => t('FileField Paths'),
'default_hook' => 'default_filefield_paths',
'feature_source' => TRUE,
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
),
);
}
/**
* Implements hook_features_export_options().
*
* List all FileField Paths config settings currently available for export. This
* adds each of the configurations to the features UI where they can be chosen
* for bundling.
*
* This UI may be entirely unneccessary if we only ever export as a part of
* something else (individual fields settings), but it's here for completeness.
*
* @return array A keyed array of items, suitable for use with a FormAPI select
* or checkboxes element.
*/
function filefield_paths_features_export_options() {
$options = array();
$results = db_select('filefield_paths', 'f')
->fields('f', array('type', 'field'))
->execute();
foreach ($results as $row) {
// Generate the unique keys that can identify the row
// "{entity_type}::{bundle}::{field_name}" eg "node::story::field_illustration"
$key = "{$row->type}::{$row->field}";
$options[$key] = $key;
}
return $options;
}
/**
* Implements hook_features_export().
*
* Process the export array for a given component.
*
* Normally, we will be adding this as a child in the pipe of
* field_features_export, so that when a filefield instance is exported, this
* setting was published along with it.
*/
function filefield_paths_features_export($data, &$export, $module_name = '') {
$export['dependencies']['filefield_paths'] = 'filefield_paths';
$pipe = array();
foreach ($data as $identifier) {
if ($wrapper = filefield_paths_load($identifier)) {
$export['features']['filefield_paths'][$identifier] = $identifier;
if ($wrapper['filename']['transliterate'] || $wrapper['filepath']['transliterate']) {
$export['dependencies']['transliteration'] = 'transliteration';
}
if ($wrapper['filename']['pathauto'] || $wrapper['filepath']['pathauto']) {
$export['dependencies']['pathauto'] = 'pathauto';
}
if (module_exists('strongarm')) {
$variable_map = features_get_default_map('variable');
$pipe['variable'][] = "ffp_{$wrapper['type']}_{$wrapper['field']}";
}
}
}
return $pipe;
}
/**
* Attach our own export routine as a piped export that happens below any
* filefield path that is getting exported.
*
* The component name for fields is 'field'.
*
* HOOK_features_pipe_COMPONENT_alter()
*
* This captures each field export, and adds ourself to the dependencies and
* exports when that field is exported.
*/
function filefield_paths_features_pipe_field_alter(&$pipe, $data, $export) {
foreach ($data as $field_identifier) {
// Convert field identifier to FileField Paths format.
$field_identifier = str_replace('-', '::', $field_identifier);
// Field export is exporting a field named $field_identifier.
// If that is a filefield, we should attach ourselves as a subprocess (pipe).
// .. actually, don't need to check the field type,
// just see if we have some filefield_path
// settings that use the same $field_identifier key!
if (filefield_paths_load($field_identifier)) {
// So add this setting as a piped child of the filed when it gets exported.
$pipe['filefield_paths'][$field_identifier] = $field_identifier;
}
}
}
/**
* Return the required path settings for the named filefield instance.
*
* A CRUD utility for filefield_paths
*
* @param a unique identifier for the given field instance -
* {$entity_type::$bundle::$field}.
*
* @return a row array from the filefield_paths DB table - with the 'serialized'
* blobs unpacked.
*/
function filefield_paths_load($identifier) {
list($entity_type, $bundle, $field_name) = explode('::', $identifier);
$result = db_select('filefield_paths', 'f')
->fields('f')
->condition('type', "{$entity_type}::{$bundle}")
->condition('field', $field_name)
->execute();
if ($row = $result->fetchAssoc()) {
$ffp = array();
// Each cell in the row gets exposed, retrieve the schema to figure this out.
$schema = drupal_get_schema('filefield_paths');
foreach ($schema['fields'] as $field => $field_def) {
$ffp[$field] = empty($field_def['serialize']) ? $row[$field] : unserialize($row[$field]);
}
return $ffp;
}
return NULL;
}
/**
* Delete the identified row
*
* A CRUD utility for filefield_paths
*/
function filefield_paths_delete($identifier) {
list($entity_type, $bundle, $field_name) = explode('::', $identifier);
db_delete('filefield_paths')
->condition('type', "{$entity_type}::{$bundle}")
->condition('field', $field_name)
->execute();
}
/**
* Implements hook_features_export_render()
*
* Return the PHP code that represents a dump of the settings listed as $data
*/
function filefield_paths_features_export_render($module, $data) {
$code = array();
$code[] = ' $settings = array();';
$code[] = '';
$translatables = array();
foreach ($data as $item_id) {
$item = filefield_paths_load($item_id);
if (empty($item)) {
watchdog('filefield_paths', "Failed to retrieve the filefield path settings '%item_id' while preparing the feature export code.", array('%item_id' => $item_id), WATCHDOG_WARNING);
continue;
}
$code[] = " // Exported {$item_id}";
$export = features_var_export($item, ' ');
$code[] = " \$settings['{$item_id}'] = {$export};";
}
$code[] = '';
$code[] = ' return $settings;';
$code = implode("\n", $code);
return array('default_filefield_paths' => $code);
}
/**
* Implements hook_features_export_revert().
*/
function filefield_paths_features_revert($module) {
filefield_paths_features_rebuild($module);
}
/**
* Create/recreate the items based on the data array. Data should contain a
* number of filefield_paths definitions.
*
* Implements hook_features_export_rebuild().
*
* Data just need to be put straight into the database as rows.
*/
function filefield_paths_features_rebuild($module) {
if ($defaults = features_get_default('filefield_paths', $module)) {
foreach ($defaults as $filefield_paths_id => $filefield_paths) {
// Delete any previous settings for this item.
if (filefield_paths_load($filefield_paths_id)) {
filefield_paths_delete($filefield_paths_id);
}
drupal_write_record('filefield_paths', $filefield_paths);
}
}
}

View File

@@ -0,0 +1,110 @@
<?php
/**
* @file
* File module integration.
*/
/**
* Implements hook_filefield_paths_form_alter().
*/
function file_filefield_paths_form_alter(&$form, &$ffp) {
if (isset($form['#field']) && $form['#field']['type'] == 'file' && isset($form['instance']['settings']['file_directory'])) {
$ffp[$form['#field']['field_name']] = array(
'show' => TRUE,
'type' => "{$form['instance']['entity_type']['#value']}::{$form['instance']['bundle']['#value']}",
'form_path' => &$form['instance']["ffp_{$form['#field']['field_name']}"],
'file_path_default' => $form['instance']['settings']['file_directory']['#default_value']
);
// Create path settings fieldset
$ffp[$form['#field']['field_name']]['form_path'] = array(
'#type' => 'fieldset',
'#title' => t('File Path settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$ffp[$form['#field']['field_name']]['form_path']['file_path'] = $form['instance']['settings']['file_directory'];
$ffp[$form['#field']['field_name']]['form_path']['file_path']['#title'] = t('File path');
$form['instance']['settings']['file_directory']['#access'] = FALSE;
}
}
/**
* Implements hook_filefield_paths_form_submit().
*/
function file_filefield_paths_form_submit(&$form_state, &$ffp) {
if (isset($form_state['values']['form_id']) && $form_state['values']['form_id'] == 'field_ui_field_edit_form') {
$form_state['values']["ffp_{$form_state['values']['instance']['field_name']}"] = $form_state['values']['instance']["ffp_{$form_state['values']['instance']['field_name']}"];
$ffp[$form_state['values']['instance']['field_name']] = array(
'type' => "{$form_state['values']['instance']['entity_type']}::{$form_state['values']['instance']['bundle']}",
);
$form_state['values']['instance']['settings']['file_directory'] = $form_state['values']["ffp_{$form_state['values']['instance']['field_name']}"]['file_path'];
}
}
/**
* Implements hook_filefield_paths_get_fields().
*/
function file_filefield_paths_get_fields(&$entity, &$ffp) {
if (is_object($entity)) {
$fields = field_info_fields();
foreach ($fields as $name => $field) {
if ($field['type'] == 'file' && isset($entity->{$field['field_name']}) && is_array($entity->{$field['field_name']})) {
foreach ($entity->{$field['field_name']} as $language) {
foreach ($language as $file) {
$new = isset($file['new']) ? $file['new'] : FALSE;
$file = file_load($file['fid']);
$ffp['#files'][] = array(
'field' => (array) $file,
'module' => $field['module'],
'name' => $field['field_name'],
'new' => $new || $file->timestamp == REQUEST_TIME,
);
$ffp['#types'][$field['field_name']] = TRUE;
}
}
}
}
}
}
/**
* Implements hook_filefield_paths_batch_update().
*/
function file_filefield_paths_batch_update($instance, &$objects) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', $instance['entity_type'])
->entityCondition('bundle', array($instance['bundle']))
->fieldCondition($instance['field_name'])
->execute();
$objects = array_keys($result[$instance['entity_type']]);
}
/**
* Implements hook_filefield_paths_update().
*/
function file_filefield_paths_update($oid, $instance) {
$entity = current(entity_load($instance['entity_type'], array($oid)));
// Flag files for update.
if (isset($entity->{$instance['field_name']})) {
foreach ($entity->{$instance['field_name']} as &$language) {
foreach ($language as &$file) {
if (!is_array($file) || empty($file['uri'])) {
continue;
}
$file['new'] = TRUE;
}
}
}
// Process Entity.
filefield_paths_entity_update($entity, $instance['entity_type']);
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* @file
* FileField Paths module integration.
*/
/**
* Implements hook_filefield_paths_field_settings().
*/
function filefield_paths_filefield_paths_field_settings() {
return array(
'file_path' => array(
'title' => 'File path',
'sql' => 'filepath',
'form' => array(
'file_path' => array(
'#maxlength' => 512,
'#size' => 128,
),
),
),
'file_name' => array(
'title' => 'File name',
'sql' => 'filename',
'form' => array(
'file_name' => array(
'#type' => 'textfield',
'#title' => t('File name'),
'#default_value' => '[file:ffp-name-only-original].[file:ffp-extension-original]',
),
),
)
);
}
/**
* Implements hook_filefield_paths_process_file().
*/
function filefield_paths_filefield_paths_process_file($new, &$file, $settings, &$entity, $type, &$update) {
if ($new && !empty($file['field']['filename'])) {
$field = field_info_field($file['name']);
$token_data = array('file' => file_load($file['field']['fid']), $type => $entity);
// Process filename.
$file['filename']['old'] = $file['field']['filename'];
$file['filename']['new'] = !empty($settings['filename']['value'])
? filefield_paths_process_string($settings['filename']['value'], $token_data, $settings['filename'])
: $file['field']['filename'];
// Process filepath.
$file['filepath']['old'] = $file['field']['uri'];
$file['filepath']['new'] = "{$field['settings']['uri_scheme']}://" . filefield_paths_process_string($settings['filepath']['value'] . "/{$file['filename']['new']}", $token_data, $settings['filepath']);
// Finalize files if necessary.
$dirname = dirname($file['filepath']['new']);
if ($dirname != dirname($file['field']['uri']) || $file['filename']['new'] != $file['field']['filename']) {
if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && file_move((object) $file['field'], $file['filepath']['new'])) {
$update->entity = TRUE;
// Fix reference to old paths.
$file['filepath']['new'] = str_replace($file['filename']['old'], $file['filename']['new'], $file['filepath']['new']);
// Process regular expression.
_filefield_paths_replace_path($file['filepath']['old'], $file['filepath']['new'], $entity, $update);
// Store new filename in file Array
$file['field']['filename'] = $file['filename']['new'];
$file['field']['uri'] = $file['filepath']['new'];
}
}
}
}
/**
* Implements hook_filefield_paths_cleanup().
*/
function filefield_paths_filefield_paths_cleanup($ffp, $name) {
foreach ($ffp['#files'] as $file) {
if (isset($file['filepath'])) {
$scheme = file_uri_scheme($file['filepath']['old']);
$paths = explode('/', str_replace("{$scheme}://", '', dirname($file['filepath']['old'])));
while ($paths) {
if (drupal_rmdir("{$scheme}://" . implode('/', $paths)) == TRUE) {
array_pop($paths);
continue;
}
break;
}
}
}
}

View File

@@ -0,0 +1,110 @@
<?php
/**
* @file
* Image module integration.
*/
/**
* Implements hook_filefield_paths_form_alter().
*/
function image_filefield_paths_form_alter(&$form, &$ffp) {
if (isset($form['#field']) && $form['#field']['type'] == 'image' && isset($form['instance']['settings']['file_directory'])) {
$ffp[$form['#field']['field_name']] = array(
'show' => TRUE,
'type' => "{$form['instance']['entity_type']['#value']}::{$form['instance']['bundle']['#value']}",
'form_path' => &$form['instance']["ffp_{$form['#field']['field_name']}"],
'file_path_default' => $form['instance']['settings']['file_directory']['#default_value']
);
// Create path settings fieldset
$ffp[$form['#field']['field_name']]['form_path'] = array(
'#type' => 'fieldset',
'#title' => t('Image Path settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$ffp[$form['#field']['field_name']]['form_path']['file_path'] = $form['instance']['settings']['file_directory'];
$ffp[$form['#field']['field_name']]['form_path']['file_path']['#title'] = t('File path');
$form['instance']['settings']['file_directory']['#access'] = FALSE;
}
}
/**
* Implements hook_filefield_paths_form_submit().
*/
function image_filefield_paths_form_submit(&$form_state, &$ffp) {
if (isset($form_state['values']['form_id']) && $form_state['values']['form_id'] == 'field_ui_field_edit_form') {
$form_state['values']["ffp_{$form_state['values']['instance']['field_name']}"] = $form_state['values']['instance']["ffp_{$form_state['values']['instance']['field_name']}"];
$ffp[$form_state['values']['instance']['field_name']] = array(
'type' => "{$form_state['values']['instance']['entity_type']}::{$form_state['values']['instance']['bundle']}",
);
$form_state['values']['instance']['settings']['file_directory'] = $form_state['values']["ffp_{$form_state['values']['instance']['field_name']}"]['file_path'];
}
}
/**
* Implements hook_filefield_paths_get_fields().
*/
function image_filefield_paths_get_fields(&$entity, &$ffp) {
if (is_object($entity)) {
$fields = field_info_fields();
foreach ($fields as $name => $field) {
if ($field['type'] == 'image' && isset($entity->{$field['field_name']}) && is_array($entity->{$field['field_name']})) {
foreach ($entity->{$field['field_name']} as $language) {
foreach ($language as $file) {
$new = isset($file['new']) ? $file['new'] : FALSE;
$file = file_load($file['fid']);
$ffp['#files'][] = array(
'field' => (array) $file,
'module' => $field['module'],
'name' => $field['field_name'],
'new' => $new || $file->timestamp == REQUEST_TIME,
);
$ffp['#types'][$field['field_name']] = TRUE;
}
}
}
}
}
}
/**
* Implements hook_filefield_paths_batch_update().
*/
function image_filefield_paths_batch_update($instance, &$objects) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', $instance['entity_type'])
->entityCondition('bundle', array($instance['bundle']))
->fieldCondition($instance['field_name'])
->execute();
$objects = array_keys($result[$instance['entity_type']]);
}
/**
* Implements hook_filefield_paths_update().
*/
function image_filefield_paths_update($oid, $instance) {
$entity = current(entity_load($instance['entity_type'], array($oid)));
// Flag files for update.
if (isset($entity->{$instance['field_name']})) {
foreach ($entity->{$instance['field_name']} as &$language) {
foreach ($language as &$file) {
if (!is_array($file) || empty($file['uri'])) {
continue;
}
$file['new'] = TRUE;
}
}
}
// Process Entity.
filefield_paths_entity_update($entity, $instance['entity_type']);
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* @file
* Token module integration.
*/
/**
* Implements hook_token_info().
*/
function filefield_paths_token_info() {
$info['tokens']['file']['ffp-name-only'] = array(
'name' => t("File name"),
'description' => t("File name without extension."),
);
$info['tokens']['file']['ffp-name-only-original'] = array(
'name' => t("File name - original"),
'description' => t("File name without extension - original."),
);
$info['tokens']['file']['ffp-extension-original'] = array(
'name' => t("File extension - original"),
'description' => t("File extension - original."),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function filefield_paths_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array('absolute' => TRUE);
if (isset($language)) {
$url_options['language'] = $language;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'file' && !empty($data['file'])) {
$file = $data['file'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'ffp-name-only':
$info = pathinfo($file->filename);
$replacements[$original] = $info['filename'];
break;
case 'ffp-name-only-original':
$info = pathinfo($file->origname);
$replacements[$original] = $info['filename'];
break;
case 'ffp-extension-original':
$info = pathinfo($file->origname);
$replacements[$original] = $info['extension'];
break;
}
}
}
return $replacements;
}