You are here

function date_repeat_entity_get_date_entities in Date Repeat Entity 7

Same name and namespace in other branches
  1. 7.2 includes/date_repeat_entity.utility.inc \date_repeat_entity_get_date_entities()

Constructs a query that searches for entities matching certain conditions.

Parameters

EntityDrupalWrapper $entity_wrapper: A property wrapper for an entity.

string $entity_type: The entity type to load, e.g. node.

string $bundle: The bundle type, e.g. event.

string $scope: Defines the extent to which date series should be searched. Can be one of: current (for the current date instance only), future (for the current and all future date instances), all (all instances of a date series).

bool $exclude_current_entity: Specifies whether a query that searches a date series should exclude the entity that represents the current date instance.

Return value

array Represents the result of a query.

2 calls to date_repeat_entity_get_date_entities()
date_repeat_entity_delete_dates in includes/date_repeat_entity.delete.inc
Delete dates associated with a repeating date series.
_date_repeat_entity_update_dates in includes/date_repeat_entity.update.inc
Update dates associated with a repeating date series.

File

includes/date_repeat_entity.utility.inc, line 276
Contains utility functions that support operations on entities with repeating date fields.

Code

function date_repeat_entity_get_date_entities($entity_wrapper, $entity_type, $bundle, $scope = 'current', $exclude_current_entity = FALSE) {

  // Get the series master UUID.
  $master_uuid = $entity_wrapper->field_master_uuid
    ->value();

  // Get the entity_id from the entity wrapper.
  $entity_id = $entity_wrapper
    ->getIdentifier();

  // Use EFQ to get all dates with the same field_master_uuid value.
  $query = new EntityFieldQuery();

  // Define EFQ conditions based on the entity type, bundle and master_uuid.
  $query
    ->entityCondition('entity_type', $entity_type)
    ->entityCondition('bundle', $bundle)
    ->fieldCondition('field_master_uuid', 'value', $master_uuid);

  // If necessary, exclude the subject entity from the query.
  if ($exclude_current_entity == TRUE) {
    $query
      ->propertyCondition('nid', $entity_id, '<>');
  }
  switch ($scope) {
    case 'all':

      // Don't add any more conditions.
      break;
    case 'future':

      // If scope is 'future' and a start date is passed to function,
      // add field condition to exclude earlier entities from the query;
      $repeating_date_field = date_repeat_entity_get_repeating_date_field($entity_type, $bundle);
      $repeating_date_field_name = $repeating_date_field['field_name'];

      // Retrieve start date for current entity since this is being applied
      // to all dates by the user.
      $field_data = $entity_wrapper->{$repeating_date_field_name}[0]
        ->raw();
      $start_date = $field_data['value'];
      if (!is_null($start_date)) {
        $query
          ->fieldCondition($repeating_date_field_name, 'value', $start_date, '>=');
      }
      break;
    default:

      // Default is 'current'.
      $query
        ->propertyCondition('nid', $entity_id, '=');
      break;
  }

  // Retrieve matching entities.
  $result = $query
    ->execute();
  return $result;
}