Commit cd8dd173 authored by webchick's avatar webchick

Issue #1824898 by vijaycs85, ACF: Convert drupal_weight_select_max() variable to CMI system.

parent e16159ad
......@@ -4415,7 +4415,7 @@ function form_process_weight($element) {
$element['#is_weight'] = TRUE;
// If the number of options is small enough, use a select field.
$max_elements = variable_get('drupal_weight_select_max', DRUPAL_WEIGHT_SELECT_MAX);
$max_elements = config('system.site')->get('weight_select_max');
if ($element['#delta'] <= $max_elements) {
$element['#type'] = 'select';
for ($n = (-1 * $element['#delta']); $n <= $element['#delta']; $n++) {
......
......@@ -48,32 +48,15 @@ public function execute() {
$entity_type = $this->entityType;
$entity_info = entity_get_info($entity_type);
if (!isset($entity_info['base_table'])) {
throw new QueryException("No base table, nothing to query.");
throw new QueryException("No base table, invalid query.");
}
$configurable_fields = array_map(function ($data) use ($entity_type) {
return isset($data['bundles'][$entity_type]);
}, field_info_field_map());
$base_table = $entity_info['base_table'];
// Assemble a list of entity tables, primarily for use in
// \Drupal\field_sql_storage\Entity\Tables::ensureEntityTable().
$entity_tables = array();
$simple_query = TRUE;
// ensureEntityTable() decides whether an entity property will be queried
// from the data table or the base table based on where it finds the
// property first. The data table is prefered, which is why it gets added
// before the base table.
if (isset($entity_info['data_table'])) {
$entity_tables[$entity_info['data_table']] = drupal_get_schema($entity_info['data_table']);
$simple_query = FALSE;
}
$entity_tables[$base_table] = drupal_get_schema($base_table);
$sqlQuery = $this->connection->select($base_table, 'base_table', array('conjunction' => $this->conjunction));
$sqlQuery->addMetaData('configurable_fields', $configurable_fields);
$sqlQuery->addMetaData('entity_type', $entity_type);
// Determines the key of the column to join on. This is either the entity
// id key or the revision id key, depending on whether the entity type
// supports revisions.
$id_key = 'id';
$id_field = $entity_info['entity_keys']['id'];
$fields[$id_field] = TRUE;
if (empty($entity_info['entity_keys']['revision'])) {
......@@ -87,10 +70,6 @@ public function execute() {
$revision_field = $entity_info['entity_keys']['revision'];
$fields[$revision_field] = TRUE;
$sqlQuery->addField('base_table', $revision_field);
// Now revision id is column 0 and the value column is 1.
if ($this->age == FIELD_LOAD_CURRENT) {
$id_key = 'revision';
}
}
// Now add the value column for fetchAllKeyed(). This is always the
// entity id.
......@@ -116,14 +95,7 @@ public function execute() {
}
// This now contains first the table containing entity properties and
// last the entity base table. They might be the same.
$sqlQuery->addMetaData('entity_tables', $entity_tables);
$sqlQuery->addMetaData('age', $this->age);
// This contains the relevant SQL field to be used when joining entity
// tables.
$sqlQuery->addMetaData('entity_id_field', $entity_info['entity_keys'][$id_key]);
// This contains the relevant SQL field to be used when joining field
// tables.
$sqlQuery->addMetaData('field_id_field', $id_key == 'id' ? 'entity_id' : 'revision_id');
$sqlQuery->addMetaData('simple_query', $simple_query);
$this->condition->compile($sqlQuery);
if ($this->count) {
......
......@@ -18,7 +18,7 @@ function __construct(Connection $connection) {
$this->connection = $connection;
}
function get($entity_type, $conjunction) {
function get($entity_type, $conjunction = 'AND') {
return new Query($entity_type, $conjunction, $this->connection);
}
}
......@@ -6,3 +6,4 @@ page:
404: ''
front: user
admin_compact_mode: '0'
weight_select_max: '100'
<?php
/**
* @file
* Definition of Drupal\Core\Entity\Tests\EntityQueryRelationshipTest.
*/
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
/**
* Tests Entity Query API relationship functionality.
*/
class EntityQueryRelationshipTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'taxonomy');
/**
* @var \Drupal\field_sql_storage\Entity\QueryFactory
*/
protected $factory;
/**
* Term entities.
*
* @var array
*/
protected $terms;
/**
* User entities.
*
* @var array
*/
public $accounts;
/**
* entity_test entities.
*
* @var array
*/
protected $entities;
/**
* The name of the taxonomy field used for test.
*
* @var string
*/
protected $fieldName;
/**
* The results returned by EntityQuery.
*
* @var array
*/
protected $queryResults;
public static function getInfo() {
return array(
'name' => 'Entity Query relationship',
'description' => 'Tests the Entity Query relationship API',
'group' => 'Entity API',
);
}
protected function setUp() {
parent::setUp();
// We want a taxonomy term reference field. It needs a vocabulary, terms,
// a field and an instance. First, create the vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'machine_name' => strtolower($this->randomName()),
));
$vocabulary->save();
// Second, create the field.
$this->fieldName = strtolower($this->randomName());
$field = array(
'field_name' => $this->fieldName,
'type' => 'taxonomy_term_reference',
);
$field['settings']['allowed_values']['vocabulary'] = $vocabulary->machine_name;
field_create_field($field);
// Third, create the instance.
$instance = array(
'entity_type' => 'entity_test',
'field_name' => $this->fieldName,
'bundle' => 'entity_test',
);
field_create_instance($instance);
// Create two terms and also two accounts.
for ($i = 0; $i <= 1; $i++) {
$term = entity_create('taxonomy_term', array(
'name' => $this->randomName(),
'vid' => $vocabulary->vid,
));
$term->save();
$this->terms[] = $term;
$this->accounts[] = $this->drupalCreateUser();
}
// Create three entity_test entities, the 0th entity will point to the
// 0th account and 0th term, the 1st and 2nd entity will point to the
// 1st account and 1st term.
for ($i = 0; $i <= 2; $i++) {
$entity = entity_create('entity_test', array());
$entity->name->value = $this->randomName();
$index = $i ? 1 : 0;
$entity->user_id->value = $this->accounts[$index]->uid;
$entity->{$this->fieldName}->tid = $this->terms[$index]->tid;
$entity->save();
$this->entities[] = $entity;
}
$this->factory = drupal_container()->get('entity.query');
}
/**
* Tests querying.
*/
public function testQuery() {
// This returns the 0th entity as that's only one pointing to the 0th
// account.
$this->queryResults = $this->factory->get('entity_test')
->condition("user_id.entity.name", $this->accounts[0]->name)
->execute();
$this->assertResults(array(0));
// This returns the 1st and 2nd entity as those point to the 1st account.
$this->queryResults = $this->factory->get('entity_test')
->condition("user_id.entity.name", $this->accounts[0]->name, '<>')
->execute();
$this->assertResults(array(1, 2));
// This returns all three entities because all of them point to an
// account.
$this->queryResults = $this->factory->get('entity_test')
->exists("user_id.entity.name")
->execute();
$this->assertResults(array(0, 1, 2));
// This returns no entities because all of them point to an account.
$this->queryResults = $this->factory->get('entity_test')
->notExists("user_id.entity.name")
->execute();
$this->assertEqual(count($this->queryResults), 0);
// This returns the 0th entity as that's only one pointing to the 0th
// term (test without specifying the field column).
$this->queryResults = $this->factory->get('entity_test')
->condition("$this->fieldName.entity.name", $this->terms[0]->name)
->execute();
$this->assertResults(array(0));
// This returns the 0th entity as that's only one pointing to the 0th
// term (test with specifying the column name).
$this->queryResults = $this->factory->get('entity_test')
->condition("$this->fieldName.tid.entity.name", $this->terms[0]->name)
->execute();
$this->assertResults(array(0));
// This returns the 1st and 2nd entity as those point to the 1st term.
$this->queryResults = $this->factory->get('entity_test')
->condition("$this->fieldName.entity.name", $this->terms[0]->name, '<>')
->execute();
$this->assertResults(array(1, 2));
}
/**
* Assert the results.
*
* @param array $expected
* A list of indexes in the $this->entities array.
*/
protected function assertResults($expected) {
$this->assertEqual(count($this->queryResults), count($expected));
foreach ($expected as $key) {
$id = $this->entities[$key]->id();
$this->assertEqual($this->queryResults[$id], $id);
}
}
}
......@@ -2292,6 +2292,18 @@ function system_update_8038() {
));
}
/**
* Coverts drupal_weight_select_max variable to config.
*
* @ingroup config_upgrade
*/
function system_update_8039() {
$variable_map = array(
'drupal_weight_select_max' => 'weight_select_max',
);
update_variables_to_config('system.site', $variable_map);
}
/**
* @} End of "defgroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
......
......@@ -46,13 +46,6 @@
*/
const DRUPAL_REQUIRED = 2;
/**
* Maximum number of values in a weight select element.
*
* If the number of values is over the maximum, a text field is used instead.
*/
const DRUPAL_WEIGHT_SELECT_MAX = 100;
/**
* Return only visible regions.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment