/**
  * {@inheritdoc}
  */
 public function getQuery()
 {
     $query = parent::getQuery();
     // Add a query for meter_category.
     $field = field_info_field('field_group_node');
     $table_name = _field_sql_storage_tablename($field);
     $request = $this->getRequest();
     $query->leftJoin($table_name, 'gn', "message.mid = gn.entity_id AND gn.entity_type='message'");
     $query->innerJoin('field_data_field_node', 'fn', "message.mid = fn.entity_id AND fn.entity_type='message'");
     $query->innerJoin('node', 'node', "fn.field_node_target_id = node.nid");
     // Show only publish content in active stream.
     $query->condition('node.status', 1);
     if (!empty($request['topics'])) {
         // Join related to Articles tables to get V&V activities with user's
         // topics of interest.
         $query->innerJoin('field_data_c4m_vocab_topic', 'crt', "node.nid = crt.entity_id AND crt.entity_type='node'");
     }
     $query->addField('gn', 'field_group_node_target_id', 'group_node');
     if (!empty($request['group'])) {
         if (empty($request['hide_articles'])) {
             $or = db_or();
             $or->condition('gn.field_group_node_target_id', $request['group'], is_array($request['group']) ? 'IN' : '=');
             if (!empty($request['topics'])) {
                 $and = db_and();
                 $and->isNull('gn.field_group_node_target_id');
                 $and->condition('node.type', 'article');
                 $and->condition('crt.c4m_vocab_topic_tid', $request['topics'], is_array($request['topics']) ? 'IN' : '=');
                 $or->condition($and);
             } else {
                 $or->isNull('gn.field_group_node_target_id');
             }
             $query->condition($or);
         } else {
             $query->condition('gn.field_group_node_target_id', $request['group'], is_array($request['group']) ? 'IN' : '=');
         }
     }
     $query->addTag('activity_stream_entity_field_access');
     return $query;
 }
/**
 * Remove field storage information when field data is purged.
 *
 * Called from field_purge_data() to allow the field storage
 * module to delete field data information.
 *
 * @param $entity_type
 *   The type of $entity; for example, 'node' or 'user'.
 * @param $entity
 *   The pseudo-entity whose field data to delete.
 * @param $field
 *   The (possibly deleted) field whose data is being purged.
 * @param $instance
 *   The deleted field instance whose data is being purged.
 */
function hook_field_storage_purge($entity_type, $entity, $field, $instance)
{
    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
    $table_name = _field_sql_storage_tablename($field);
    $revision_name = _field_sql_storage_revision_tablename($field);
    db_delete($table_name)->condition('entity_type', $entity_type)->condition('entity_id', $id)->execute();
    db_delete($revision_name)->condition('entity_type', $entity_type)->condition('entity_id', $id)->execute();
}
 public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false)
 {
     /* @var $node FieldNode */
     $object = $node->getValue();
     if (!is_array($object)) {
         $object = array();
     }
     if (!isset($object['type'])) {
         $context->logCritical(sprintf("%s: has no type", $node->getPath()));
     }
     $name = $node->getName();
     $type = $object['type'];
     $typeInfo = field_info_field_types($type);
     if (empty($typeInfo)) {
         $context->logCritical(sprintf("%s: type %s does not exist", $node->getPath(), $type));
     }
     if ($this->exists($node, $context)) {
         $existing = $this->getExistingObject($node, $context);
     } else {
         $existing = null;
     }
     if (array_key_exists('settings', $object) && !is_array($object['settings'])) {
         $context->log(sprintf("%s: no settings provided, defaulting with empty array", $node->getPath()));
         $object['settings'] = array();
     }
     $object['field_name'] = $name;
     if (empty($object['cardinality'])) {
         $object['cardinality'] = 1;
     }
     // Consistency check, prior to do anything, ensure the database tables
     // are not already there, this happens, sometimes
     if (!$existing) {
         if (empty($object['storage']) || 'field_sql_storage' === $object['storage']['type']) {
             // Prevents warning on unspecified key
             if (!isset($object['deleted'])) {
                 $object['deleted'] = false;
             }
             $tables = [_field_sql_storage_tablename($object), _field_sql_storage_revision_tablename($object)];
             foreach ($tables as $table) {
                 if (db_table_exists($table)) {
                     $context->logDataloss(sprintf("%s: %s: table already exists prior to creating field", $node->getPath(), $table));
                     // If code has not broken here, then go for deletion
                     db_drop_table($table);
                 }
             }
         }
     }
     if ($existing) {
         $doDelete = false;
         $eType = $existing['type'];
         // Ensure the cardinality change if any is safe to proceed with
         $cardinality = $object['cardinality'] - $existing['cardinality'];
         if (0 !== $cardinality) {
             if (0 < $cardinality || -1 == $object['cardinality']) {
                 $context->log(sprintf("%s: safe cardinality change", $node->getPath()));
             } else {
                 // @todo Ensure there is data we can save in field
                 if (false) {
                     $context->log(sprintf("%s: safe cardinality change due to data shape", $node->getPath()));
                 } else {
                     $context->logDataloss(sprintf("%s: unsafe cardinality change", $node->getPath()));
                 }
             }
         }
         if ($type !== $eType) {
             $doDelete = true;
             $instances = $this->getInstances($name);
             if (empty($instances)) {
                 $context->logWarning(sprintf("%s: type change (%s -> %s): no instances", $node->getPath(), $type, $eType));
             } else {
                 // @todo Ensure there is data if there is instances
                 if (false) {
                     $context->logWarning(sprintf("%s: type change (%s -> %s): existing instances are empty", $node->getPath(), $type, $eType));
                 } else {
                     // @todo Safe should ensure schema is the same
                     if (false) {
                         $context->logWarning(sprintf("%s: type change (%s -> %s): field schema is the same", $node->getPath(), $type, $eType));
                     } else {
                         $context->logDataloss(sprintf("%s: type change (%s -> %s): data loss detected", $node->getPath(), $type, $eType));
                     }
                 }
             }
         }
         if ($doDelete) {
             $this->deleteExistingObject($node, $context);
             field_create_field($object);
             // @todo Recreate instances
         } else {
             field_update_field($object);
         }
     } else {
         field_create_field($object);
     }
 }
Example #4
0
 /**
  * Purge all data from a field instance.
  *
  * @param array $instance
  *   The field instance definition. This may be deleted or inactive.
  */
 public static function purgeInstanceData(array $instance)
 {
     $field = static::readFieldByID($instance['field_id']);
     $data_table = _field_sql_storage_tablename($field);
     // Ensure the entity caches are cleared for the changed entities.
     if ($ids = db_query("SELECT entity_id FROM {$data_table} WHERE entity_type = :type AND bundle = :bundle", array(':type' => $instance['entity_type'], ':bundle' => $instance['bundle']))->fetchCol()) {
         entity_get_controller($instance['entity_type'])->resetCache($ids);
         db_delete($data_table)->condition('entity_type', $instance['entity_type'])->condition('bundle', $instance['bundle'])->execute();
     }
     $revision_table = _field_sql_storage_revision_tablename($field);
     if (db_table_exists($revision_table)) {
         db_delete($revision_table)->condition('entity_type', $instance['entity_type'])->condition('bundle', $instance['bundle'])->execute();
     }
     watchdog('helper', "Purged data for field instance ID {$instance['id']}.");
 }
/**
 * Temporary helper to re-create equivalent 
 * of content_database_info.
 */
function date_api_database_info($field)
{
    $data = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT];
    $db_info = array('columns' => $data);
    $current_table = _field_sql_storage_tablename($field);
    $revision_table = _field_sql_storage_revision_tablename($field);
    $db_info['table'] = $current_table;
    return $db_info;
}
 public static function changeInstanceField(array $instance, $new_field_name)
 {
     $old_field = field_info_field($instance['field_name']);
     $new_field = field_info_field($new_field_name);
     if ($old_field['type'] != $new_field['type']) {
         throw new FieldException("Cannot change field instance because they are not the same field type.");
     }
     if ($old_field['storage']['type'] !== 'field_sql_storage') {
         throw new FieldException("Unable to change field type for field {$old_field['field_name']} using storage {$old_field['storage']['type']}.");
     }
     if ($new_field['storage']['type'] !== 'field_sql_storage') {
         throw new FieldException("Unable to change field type for field {$new_field['field_name']} using storage {$new_field['storage']['type']}.");
     }
     if (!field_info_instance($instance['entity_type'], $new_field_name, $instance['bundle'])) {
         $new_instance = $instance;
         $new_instance['field_name'] = $new_field_name;
         field_create_instance($new_instance);
         watchdog('helper', "Created new field instance: {$instance['entity_type']}.{$instance['bundle']}.{$new_field_name}");
     }
     // Copy data from old field tables to the new field tables.
     $old_data_table = _field_sql_storage_tablename($old_field);
     $new_data_table = _field_sql_storage_tablename($new_field);
     $query = db_select($old_data_table, 'old');
     $query->fields('old');
     $query->condition('entity_type', $instance['entity_type']);
     $query->condition('bundle', $instance['bundle']);
     db_insert($new_data_table)->from($query)->execute();
     $old_revision_table = _field_sql_storage_revision_tablename($old_field);
     if (db_table_exists($old_revision_table)) {
         $new_revision_table = _field_sql_storage_revision_tablename($new_field);
         $query = db_select($old_revision_table, 'old');
         $query->fields('old');
         $query->condition('entity_type', $instance['entity_type']);
         $query->condition('bundle', $instance['bundle']);
         db_insert($new_revision_table)->from($query)->execute();
     }
     FieldHelper::deleteInstance($instance);
 }
 /**
  * {@inheritdoc}
  */
 public function finishQuery($select_query, $id_key = 'entity_id')
 {
     $entity_type = $this->entityConditions['entity_type']['value'];
     foreach ($this->getRelationships() as $delta => $relationship) {
         // A relational filter consists of a chain of relationships and a value
         // for a condition at the end.
         // Relationships start with the entity base table.
         $entity_info = entity_get_info($entity_type);
         $entity_table = $entity_table_alias = $entity_info['base table'];
         // Add the table if the base entity table was not added because:
         // 1. There was a fieldCondition or fieldOrderBy, AND
         // 2. There was no property condition ot order.
         if ($delta == 0) {
             $is_entity_table_present = FALSE;
             $field_base_table_alias = NULL;
             foreach ($select_query->getTables() as $table_info) {
                 // Search for the base table and check if the entity table is present
                 // for the resource's entity type.
                 if (!$field_base_table_alias && empty($table_info['join type'])) {
                     $field_base_table_alias = $table_info['alias'];
                 }
                 if ($table_info['table'] == $entity_table) {
                     $is_entity_table_present = TRUE;
                     break;
                 }
             }
             if (!$is_entity_table_present && $field_base_table_alias) {
                 // We have the base table and we need to join it to the entity table.
                 _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table_alias);
             }
         }
         // Pop the last item, since it is the one that has to match the filter and
         // will have the WHERE associated.
         $condition = array_pop($relationship['relational_filters']);
         foreach ($relationship['relational_filters'] as $relational_filter) {
             /* @var RelationalFilterInterface $relational_filter */
             if ($relational_filter->getType() == RelationalFilterInterface::TYPE_FIELD) {
                 $field_table_name = _field_sql_storage_tablename(field_info_field($relational_filter->getName()));
                 $field_table_alias = $this::aliasJoinTable($field_table_name, $select_query);
                 $select_query->addJoin('INNER', $field_table_name, $field_table_alias, sprintf('%s.%s = %s.%s', $entity_table_alias, $entity_info['entity keys']['id'], $field_table_alias, $id_key));
                 // Get the entity type being referenced.
                 $entity_info = entity_get_info($relational_filter->getEntityType());
                 $entity_table_alias = $this->aliasJoinTable($entity_info['base table'], $select_query);
                 $select_query->addJoin('INNER', $entity_info['base table'], $entity_table_alias, sprintf('%s.%s = %s.%s', $field_table_name, _field_sql_storage_columnname($relational_filter->getName(), $relational_filter->getColumn()), $entity_table_alias, $entity_info['entity keys']['id']));
             } elseif ($relational_filter->getType() == RelationalFilterInterface::TYPE_PROPERTY) {
                 // In this scenario we want to join with the new table entity. This
                 // will only work if the property contains the referenced entity ID
                 // (which is not unreasonable).
                 $host_entity_table = $entity_table;
                 $entity_info = entity_get_info($relational_filter->getEntityType());
                 $entity_table_alias = $this->aliasJoinTable($entity_info['base table'], $select_query);
                 $select_query->addJoin('INNER', $entity_info['base table'], $entity_table_alias, sprintf('%s.%s = %s.%s', $host_entity_table, $relational_filter->getName(), $entity_table_alias, $entity_info['entity keys']['id']));
             }
         }
         /* @var RelationalFilterInterface $condition */
         if ($condition->getType() == RelationalFilterInterface::TYPE_FIELD) {
             // Make the join to the filed table for the condition.
             $field_table_name = _field_sql_storage_tablename(field_info_field($condition->getName()));
             $field_column = _field_sql_storage_columnname($condition->getName(), $condition->getColumn());
             $field_table_alias = $this::aliasJoinTable($field_table_name, $select_query);
             $select_query->addJoin('INNER', $field_table_name, $field_table_alias, sprintf('%s.%s = %s.%s', $entity_table_alias, $entity_info['entity keys']['id'], $field_table_alias, $id_key));
             if (in_array($relationship['operator'], array('IN', 'BETWEEN'))) {
                 $select_query->condition($field_table_name . '.' . $field_column, $relationship['value'], $relationship['operator'][0]);
             } else {
                 for ($index = 0; $index < count($relationship['value']); $index++) {
                     $select_query->condition($field_table_name . '.' . $field_column, $relationship['value'][$index], $relationship['operator'][$index]);
                 }
             }
         } elseif ($condition->getType() == RelationalFilterInterface::TYPE_PROPERTY) {
             if (in_array($relationship['operator'], array('IN', 'BETWEEN'))) {
                 $select_query->condition($entity_table_alias . '.' . $condition->getName(), $relationship['value'], $relationship['operator'][0]);
             } else {
                 for ($index = 0; $index < count($relationship['value']); $index++) {
                     $select_query->condition($entity_table_alias . '.' . $condition->getName(), $relationship['value'][$index], $relationship['operator'][$index]);
                 }
             }
         }
     }
     return parent::finishQuery($select_query, $id_key);
 }
 /**
  * Prepare data for summary section.
  *
  * Prepare a list of sub-categories and their total electricity (kWh)
  * consumption. The summary will be used by the formatter to add a 'summary'
  * section at the end of the RESTFUL reply.
  *
  * @param $query
  */
 protected function addQueryForCategoryAndAccount($query)
 {
     // Add a query for meter_category.
     $field = field_info_field(OG_VOCAB_FIELD);
     $table_name = _field_sql_storage_tablename($field);
     $query->leftJoin($table_name, 'cat', 'cat.entity_id=meter_nid');
     $query->addField('cat', 'og_vocabulary_target_id', 'meter_category');
     // Add a query for meter_account.
     $table_name = 'og_membership';
     $query->leftJoin($table_name, 'acc', 'acc.etid=meter_nid');
     $query->addField('acc', 'gid', 'meter_account');
 }