You are here

protected function EntityReference_SelectionHandler_DDF::handleDynamicArgs in Dynamic dependent fields 7

2 calls to EntityReference_SelectionHandler_DDF::handleDynamicArgs()
EntityReference_SelectionHandler_DDF::getReferencableEntities in modules/ddf_entityreference/plugins/selection/EntityReference_SelectionHandler_DDF.class.php
Implements EntityReferenceHandler::getReferencableEntities().
EntityReference_SelectionHandler_DDF::validateReferencableEntities in modules/ddf_entityreference/plugins/selection/EntityReference_SelectionHandler_DDF.class.php
Validate that entities can be referenced by this field.

File

modules/ddf_entityreference/plugins/selection/EntityReference_SelectionHandler_DDF.class.php, line 154

Class

EntityReference_SelectionHandler_DDF
Entity handler for Views.

Code

protected function handleDynamicArgs($args) {
  $dynamic_args = array();
  foreach ($args as $key => $arg) {
    $matches = array();
    if (preg_match('/^\\{([^{}]+)\\}$/', $arg, $matches)) {
      $dynamic_args[$key] = $matches[1];
    }
  }
  if (empty($dynamic_args)) {
    return $args;
  }
  $entity_id = 0;
  if (!is_null($this->entity)) {
    list($entity_id, , ) = entity_extract_ids($this->entity_type, $this->entity);
    if (empty($entity_id)) {
      $entity_id = 0;
    }
  }
  foreach ($dynamic_args as $key => $field_name) {
    $field = field_info_field($field_name);
    if (!$field) {
      $args[$key] = '';
      continue;
    }

    // Workaround for possible Entity reference weirdness with field columns.
    $columns = $field['type'] === 'entityreference' ? array(
      'target_id',
    ) : array_keys($field['columns']);
    if (count($columns) != 1) {
      drupal_alter('ddf_entityreference_master_field_columns', $columns, $field);
      if (count($columns) != 1) {
        $args[$key] = '';
        continue;
      }
    }
    $column = $columns[0];
    if (isset(self::$controlling_field_values[$this->entity_type][$entity_id]) && array_key_exists($field_name, self::$controlling_field_values[$this->entity_type][$entity_id])) {
      $args[$key] = self::$controlling_field_values[$this->entity_type][$entity_id][$field_name];
    }
    elseif (isset($this->entity->{$field_name})) {
      foreach ($this->entity->{$field_name} as $values) {
        foreach ($values as $value) {
          if (!is_array($value)) {
            $args[$key] = $value;
          }
          elseif (array_key_exists($column, $value)) {
            $args[$key] = $value[$column];
          }
        }
      }
    }
    else {
      $args[$key] = '';
      $instance = field_info_instance($this->instance['entity_type'], $field_name, $this->instance['bundle']);
      $default_values = field_get_default_value($this->instance['entity_type'], $this->entity, $field, $instance);
      if (!empty($default_values)) {
        foreach ($default_values as $value) {
          if (array_key_exists($column, $value)) {
            $args[$key] = $value[$column];
          }
        }
      }
    }
    if (array_key_exists($key, $args)) {
      drupal_alter('ddf_entityreference_arg_value', $args[$key], $field);
    }
  }
  return $args;
}