/** * Called to add the field to a query. * * By default, the only columns added to the query are entity_id and * entity_type. This is because other needed data is fetched by entity_load(). * Other columns are added only if they are used in groupings, or if * 'add fields to query' is specifically set to TRUE in the field definition. * * The 'add fields to query' switch is used by modules which need all data * present in the query itself (such as "sphinx"). */ function query($use_groupby = FALSE) { $this->get_base_table(); $params = array(); if ($use_groupby) { // When grouping on a "field API" field (whose "real_field" is set to // entity_id), retrieve the minimum entity_id to have a valid entity_id to // pass to field_view_field(). $params = array('function' => 'min'); $this->ensure_my_table(); } // Get the entity type according to the base table of the field. // Then add it to the query as a formula. That way we can avoid joining // the field table if all we need is entity_id and entity_type. $entity_type = $this->definition['entity_tables'][$this->base_table]; $entity_info = entity_get_info($entity_type); if (isset($this->relationship)) { $this->base_table_alias = $this->relationship; } else { $this->base_table_alias = $this->base_table; } // We always need the base field (entity_id / revision_id). if (empty($this->definition['is revision'])) { $this->field_alias = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['id'], '', $params); } else { $this->field_alias = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['revision'], '', $params); $this->aliases['entity_id'] = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['id'], '', $params); } // The alias needs to be unique, so we use both the field table and the entity type. $entity_type_alias = $this->definition['table'] . '_' . $entity_type . '_entity_type'; $this->aliases['entity_type'] = $this->query->add_field(NULL, "'{$entity_type}'", $entity_type_alias); $fields = $this->additional_fields; // We've already added entity_type, so we can remove it from the list. $entity_type_key = array_search('entity_type', $fields); if ($entity_type_key !== FALSE) { unset($fields[$entity_type_key]); } if ($use_groupby) { // Add the fields that we're actually grouping on. $options = array(); if ($this->options['group_column'] != 'entity_id') { $options = array($this->options['group_column'] => $this->options['group_column']); } $options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : array(); $fields = array(); $rkey = $this->definition['is revision'] ? 'FIELD_LOAD_REVISION' : 'FIELD_LOAD_CURRENT'; // Go through the list and determine the actual column name from field api. foreach ($options as $column) { $name = $column; if (isset($this->multifield_info['storage']['details']['sql'][$rkey][$this->table][$column])) { $name = $this->multifield_info['storage']['details']['sql'][$rkey][$this->table][$column]; } $fields[$column] = $name; } $this->group_fields = $fields; } // Add additional fields (and the table join itself) if needed. if ($this->add_field_table($use_groupby)) { $this->ensure_my_table(); $this->add_additional_fields($fields); // Filter by language, if field translation is enabled. $field = $this->multifield_info; if (field_is_translatable($entity_type, $field) && !empty($this->view->display_handler->options['field_language_add_to_query'])) { $column = $this->table_alias . '.language'; // By the same reason as field_language the field might be LANGUAGE_NONE in reality so allow it as well. // @see this::field_language() global $language_content; $default_language = language_default('language'); $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'), array($language_content->language, $default_language), $this->view->display_handler->options['field_language']); $placeholder = $this->placeholder(); $language_fallback_candidates = array($language); if (variable_get('locale_field_language_fallback', TRUE)) { require_once DRUPAL_ROOT . '/includes/language.inc'; $language_fallback_candidates = array_merge($language_fallback_candidates, language_fallback_get_candidates()); } else { $language_fallback_candidates[] = LANGUAGE_NONE; } $this->query->add_where_expression(0, "{$column} IN({$placeholder}) OR {$column} IS NULL", array($placeholder => $language_fallback_candidates)); } } // The revision id inhibits grouping. // So, stop here if we're using grouping, or if aren't adding all columns to // the query. if ($use_groupby || empty($this->definition['add fields to query'])) { return; } $this->add_additional_fields(array('revision_id')); }
/** * Returns the entity language. * * @return string * The entity language. */ public function getEntityLanguage() { if (field_is_translatable($this->entityType, $this->fieldInfo)) { return entity_language($this->entityType, $this->entity); } return LANGUAGE_NONE; }
public function post($route, $form) { global $user; if ($route == 'comments.json') { $options = $this->getOptions(); $node = node_load($options['nid']); if ($options['uid'] != $user->uid || !is_object($node)) { return false; } // Should we let the comment pass ? if ($node->comment != COMMENT_NODE_OPEN || !user_access('post comments')) { // Access denied. return false; } if (!empty($form->values['cid'])) { $comment = comment_load($form->values['cid']); if (!is_object($comment)) { // Not existent CID.. Access denied return false; } $nodeSubmittedComment = node_load($comment->nid); if (!is_object($nodeSubmittedComment) || $nodeSubmittedComment->nid != $node->nid) { return FALSE; // BAD nid.. Or node non existent } // Publish if ($form->values['toPublish']) { if (user_access('administer comments') && user_access('post comments')) { $comment->status = COMMENT_PUBLISHED; comment_save($comment); } return; } // Deletion if ($form->values['toDelete']) { if (user_access('administer comments') && user_access('post comments')) { comment_delete($comment->cid); } return; } if (!comment_access('edit', $comment)) { return FALSE; // No access to edit the comment. } } if (empty($comment)) { $pid = NULL; if (!empty($form->values['pid'])) { if ($form->values['pid'] == (int) $form->values['pid']) { if ($comment_parent = comment_load((int) $form->values['pid'])) { $pid = $form->values['pid']; } } } $comment = new stdClass(); $comment->nid = $node->nid; $comment->pid = $pid; $comment->uid = $user->uid; $comment->name = check_plain($form->values['author']); } $comment->subject = check_plain($form->values['subject']); $field = field_info_field('comment_body'); $langcode = field_is_translatable('comment', $field) ? entity_language('comment', $comment) : LANGUAGE_NONE; $field_infos = field_info_instance('comment', 'comment_body', 'comment_node_' . $node->type); $format = $options['comment-body-format']; $text_processing = $field_infos['settings']['text_processing']; $body = $form->values['body']; $body = $format != 'plain_text' && $text_processing ? check_markup($body, $format) : check_plain($body); if ($text_processing) { $comment->comment_body[$langcode][0]['format'] = $format; } $comment->comment_body = array($langcode => array()); $comment->comment_body[$langcode][0]['value'] = $body; comment_submit($comment); comment_save($comment); cache_clear_all(); } }