You are here

function drush_filefield_paths_ffp_update in File (Field) Paths 8

Same name and namespace in other branches
  1. 7 filefield_paths.drush.inc \drush_filefield_paths_ffp_update()

Retroactively updates all File (Field) Paths of a chosen field instance.

Parameters

mixed|null $entity_type: Entity type.

mixed|null $bundle_name: Bundle name.

mixed|null $field_name: Field name.

Return value

string Command result.

File

./filefield_paths.drush.inc, line 48
Drush integration.

Code

function drush_filefield_paths_ffp_update($entity_type = NULL, $bundle_name = NULL, $field_name = NULL) {

  // Build array of information of all entity types, bundle names and field
  // names.
  $field_types = array_keys(_filefield_paths_get_field_types());
  $info = [];
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $field_types)) {
      foreach ($field['bundles'] as $entity_type_name => $bundles) {
        if (!isset($info[$entity_type_name])) {
          $entity_type_info = entity_get_info($entity_type_name);
          $info[$entity_type_name] = [
            '#label' => "{$entity_type_info['label']} ({$entity_type_name})",
          ];
        }
        $bundles_info = field_info_bundles($entity_type_name);
        foreach ($bundles as $bundle) {
          if (!isset($info[$entity_type_name][$bundle])) {
            $info[$entity_type_name][$bundle] = [
              '#label' => "{$bundles_info[$bundle]['label']} ({$bundle})",
            ];
          }
          $field = field_info_instance($entity_type_name, $field['field_name'], $bundle);
          $info[$entity_type_name][$bundle][$field['field_name']] = "{$field['label']} ({$field['field_name']})";
        }
      }
    }
  }
  if (drush_get_option('all')) {
    $instances = [];
    foreach ($info as $entity_type => $bundles) {
      foreach (element_children($bundles) as $bundle_name) {
        foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
          $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
        }
      }
    }
    _filefield_paths_drush_ffp_update($instances);
    return '';
  }

  // Get entity type.
  if (empty($entity_type)) {
    $choices = [
      'all' => dt('All'),
    ];
    foreach ($info as $entity_type => $entity_type_info) {
      $choices[$entity_type] = $entity_type_info['#label'];
    }
    $entity_type = drush_choice($choices, dt("Choose an Entity type."));
  }
  if (empty($entity_type)) {
    return '';
  }
  if ($entity_type == 'all') {
    $instances = [];
    foreach ($info as $entity_type => $bundles) {
      foreach (element_children($bundles) as $bundle_name) {
        foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
          $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
        }
      }
    }
    _filefield_paths_drush_ffp_update($instances);
    return '';
  }

  // Get bundle.
  if (empty($bundle_name)) {
    $choices = [
      'all' => dt('All'),
    ];
    foreach (element_children($info[$entity_type]) as $bundle_name) {
      $choices[$bundle_name] = $info[$entity_type][$bundle_name]['#label'];
    }
    $bundle_name = drush_choice($choices, dt("Choose a Bundle."));
  }
  if (empty($bundle_name)) {
    return '';
  }
  if ($bundle_name == 'all') {
    $instances = [];
    foreach (element_children($info[$entity_type]) as $bundle_name) {
      foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
        $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
      }
    }
    _filefield_paths_drush_ffp_update($instances);
    return '';
  }

  // Get field.
  if (empty($field_name)) {
    $choices = [
      'all' => dt('All'),
    ];
    foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
      $choices[$field_name] = $info[$entity_type][$bundle_name][$field_name];
    }
    $field_name = drush_choice($choices, dt("Choose a Field."));
  }
  if (empty($field_name)) {
    return '';
  }
  if ($field_name == 'all') {
    $instances = [];
    foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
      $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
    }
    _filefield_paths_drush_ffp_update($instances);
    return '';
  }

  // Invoke Retroactive update.
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  _filefield_paths_drush_ffp_update([
    $instance,
  ]);
}