Guest User

Untitled

a guest
Jan 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Provides FileField Paths integration with the Image module.
  6.  */
  7.  
  8. /**
  9.  * Implements hook_filefield_paths_form_alter().
  10.  *
  11.  * This actually adds the settings on the fields config page
  12.  */
  13. function image_filefield_paths_form_alter(&$form, &$ffp) {
  14.   if (isset($form['#field']) && $form['#field']['type'] == 'image' && isset($form['instance']['settings']['file_directory'])) {
  15.     $ffp[$form['#field']['field_name']] = array(
  16.       'show' => TRUE,
  17.       'type' => $form['instance']['bundle']['#value'],
  18.       'form_path' => &$form['instance']['ffp_'. $form['#field']['field_name']],
  19.       'file_path_default' => $form['instance']['settings']['file_directory']['#default_value']
  20.     );
  21.  
  22.     // Create path settings fieldset
  23.     $ffp[$form['#field']['field_name']]['form_path'] = array(
  24.       '#type' => 'fieldset',
  25.       '#title' => t('Image Path settings'),
  26.       '#collapsible' => TRUE,
  27.       '#collapsed' => TRUE,
  28.       '#weight' => 1,
  29.     );
  30.  
  31.     $ffp[$form['#field']['field_name']]['form_path']['file_path'] = $form['instance']['settings']['file_directory'];
  32.     $ffp[$form['#field']['field_name']]['form_path']['file_path']['#title'] = t('File path');
  33.     $form['instance']['settings']['file_directory']['#access'] = FALSE;
  34.   }
  35. }
  36.  
  37. /**
  38.  * Implements hook_filefield_paths_form_submit().
  39.  */
  40. function image_filefield_paths_form_submit(&$form_state, &$ffp) {
  41.   if (isset($form_state['values']['form_id']) && $form_state['values']['form_id'] != 'node_type_form') {
  42.     $form_state['values']['ffp_' . $form_state['values']['instance']['field_name']] = $form_state['values']['instance']['ffp_' . $form_state['values']['instance']['field_name']];
  43.     $ffp[$form_state['values']['instance']['field_name']] = array(
  44.       'type' => $form_state['values']['instance']['bundle'],
  45.     );
  46.  
  47.     $form_state['values']['instance']['settings']['file_directory'] = $form_state['values']['ffp_' . $form_state['values']['instance']['field_name']]['file_path'];
  48.   }
  49. }
  50.  
  51. /**
  52.  * Implements hook_filefield_paths_get_fields().
  53.  */
  54. function image_filefield_paths_get_fields(&$node, &$ffp) {
  55.   if (is_object($node)) {
  56.     $fields = field_info_fields();
  57.     foreach ($fields as $name => $field) {
  58.       if ($field['type'] == 'image' && isset($node->{$field['field_name']}) && is_array($node->{$field['field_name']})) {
  59.         foreach ($node->{$field['field_name']} as $language) {
  60.           foreach ($language as $file) {
  61.             $new = !isset($file['uri']) ? TRUE : FALSE;
  62.             $file = !isset($file['uri']) ? file_load($file['fid']) : (object) $file;
  63.  
  64.             $ffp['#files'][] = array(
  65.               'field' => (array) $file,
  66.               'module' => $field['module'],
  67.               'name' => $field['field_name'],
  68.               'new' => ($new || isset($file->new)) ? TRUE : FALSE,
  69.             );
  70.  
  71.             $ffp['#types'][$field['field_name']] = TRUE;
  72.           }
  73.         }
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79. /**
  80.  * Implements hook_filefield_paths_batch_update().
  81.  */
  82. function image_filefield_paths_batch_update($field, $type, &$objects) {
  83.   $query = new EntityFieldQuery();
  84.   $query->entityCondition('entity_type', 'node')
  85.     ->entityCondition('bundle', array($type))
  86.     ->fieldCondition($field);
  87.  
  88.   $results = $query->execute();
  89.  
  90.   // Build array of Node IDs.
  91.   foreach ($results['node'] as $node) {
  92.     $objects[] = $node->nid;
  93.   }
  94. }
  95.  
  96. /**
  97.  * Implements hook_filefield_paths_update().
  98.  */
  99. function image_filefield_paths_update($oid, $field) {
  100.   $node = node_load($oid);
  101.  
  102.   // Flag files for update.
  103.   if (isset($node->{$field})) {
  104.     foreach ($node->{$field} as &$language) {
  105.       foreach ($language as &$file) {
  106.  
  107.         if (!is_array($file) || empty($file['uri'])) {
  108.  
  109.           continue;
  110.         }
  111.  
  112.         $file['new'] = TRUE;
  113.       }
  114.     }
  115.   }
  116.   // Process Node.
  117.   filefield_paths_node_update($node);
  118. }
Add Comment
Please, Sign In to add comment