You are here

function duration_field_get_duration_fields in Duration Field 8.2

Same name and namespace in other branches
  1. 3.0.x duration_field.module \duration_field_get_duration_fields()

Generates an array of database tables that contain duration field data.

Return value

array An array where the key is the table name in the database, and the value is the field name.

3 calls to duration_field_get_duration_fields()
DurationFieldCommand::prepareModuleUninstall in src/Drush/DurationFieldCommand.php
Prepares the Duration Field module for uninstall.
duration_field_query_duration_string_alter in ./duration_field.module
Implements hook_query_TAG_alter().
duration_field_update_8200 in ./duration_field.install
Implements hook_update_N().

File

./duration_field.module, line 130
Holds hooks for the Duration Field module.

Code

function duration_field_get_duration_fields() {

  // Building the $duration_fields element is fairly resource intensive, so the
  // array is cached if any values are found.
  $duration_fields =& drupal_static(__FUNCTION__);
  if (!is_array($duration_fields)) {

    // Default to an empty array.
    $duration_fields = [];

    // Get the entity field manager.
    $entity_field_manager = \Drupal::service('entity_field.manager');

    // Get the bundle manager.
    $bundle_manager = \Drupal::service('entity_type.bundle.info');

    // Get the entity type manager.
    $entity_type_manager = \Drupal::entityTypeManager();

    // Get all the entity types on the system and loop through them.
    $entity_types = $entity_type_manager
      ->getDefinitions();
    foreach (array_keys($entity_types) as $entity_type) {

      // Check if the entity type has fields:
      if ($entity_types[$entity_type]
        ->getBundleEntityType()) {

        // Get all the bundles for the entity type, and loop throug them.
        $bundles = $bundle_manager
          ->getBundleInfo($entity_type);
        foreach (array_keys($bundles) as $bundle) {

          // Get all the fields on the bundle and loop through them.
          $fields = $entity_field_manager
            ->getFieldDefinitions($entity_type, $bundle);
          foreach (array_keys($fields) as $field) {

            // Check if the field is of type 'duration'.
            if ($fields[$field]
              ->getType() == 'duration') {
              $duration_fields[] = [
                'field' => $fields[$field],
                'entity_type' => $entity_type,
                'bundle' => $bundle,
              ];
            }
          }
        }
      }
    }
  }
  return $duration_fields;
}