/** * Tests that a basic taxonomy entity query works. */ function testTaxonomyEfq() { $terms = array(); for ($i = 0; $i < 5; $i++) { $term = $this->createTerm($this->vocabulary); $terms[$term->id()] = $term; } $result = \Drupal::entityQuery('taxonomy_term')->execute(); sort($result); $this->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.'); $tid = reset($result); $ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $this->vocabulary->id()); $term = _field_create_entity_from_ids($ids); $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.'); // Create a second vocabulary and five more terms. $vocabulary2 = $this->createVocabulary(); $terms2 = array(); for ($i = 0; $i < 5; $i++) { $term = $this->createTerm($vocabulary2); $terms2[$term->id()] = $term; } $result = \Drupal::entityQuery('taxonomy_term')->condition('vid', $vocabulary2->id())->execute(); sort($result); $this->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', array('%name' => $vocabulary2->label()))); $tid = reset($result); $ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $vocabulary2->id()); $term = _field_create_entity_from_ids($ids); $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.'); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $field_storage = $this->entity->getFieldStorageDefinition(); $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId()); $form_title = $this->t('%field settings for %bundle', array('%field' => $this->entity->getLabel(), '%bundle' => $bundles[$this->entity->bundle]['label'])); $form['#title'] = $form_title; if ($field_storage->isLocked()) { $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->entity->getLabel()))); return $form; } // Build the configurable field values. $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $this->entity->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20); $form['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->entity->getDescription(), '#rows' => 5, '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => $this->displayAllowedTags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10); $form['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->entity->isRequired(), '#weight' => -5); // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->entity->getTargetEntityTypeId(), 'bundle' => $this->entity->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $items = $form['#entity']->get($this->entity->getName()); $item = $items->first() ?: $items->appendItem(); // Add field settings for the field type and a container for third party // settings that modules can add to via hook_form_FORM_ID_alter(). $form['settings'] = array('#tree' => TRUE, '#weight' => 10); $form['settings'] += $item->fieldSettingsForm($form, $form_state); $form['third_party_settings'] = array('#tree' => TRUE, '#weight' => 11); // Add handling for default value. if ($element = $items->defaultValuesForm($form, $form_state)) { $element = array_merge($element, array('#type' => 'details', '#title' => $this->t('Default value'), '#open' => TRUE, '#tree' => TRUE, '#description' => $this->t('The default value for this field, used when creating new content.'))); $form['default_value'] = $element; } return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $field_label = $form_state->get('field_config')->label(); $form['#title'] = $field_label; $form['#prefix'] = '<p>' . $this->t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $field_label)) . '</p>'; // See if data already exists for this field. // If so, prevent changes to the field settings. if ($this->entity->hasData()) { $form['#prefix'] = '<div class="messages messages--error">' . $this->t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['#prefix']; } // Add settings provided by the field module. The field module is // responsible for not returning settings that cannot be changed if // the field already has data. $form['settings'] = array('#weight' => -10, '#tree' => TRUE); // Create an arbitrary entity object, so that we can have an instantiated // FieldItem. $ids = (object) array('entity_type' => $form_state->get('entity_type_id'), 'bundle' => $form_state->get('bundle'), 'entity_id' => NULL); $entity = _field_create_entity_from_ids($ids); $items = $entity->get($this->entity->getName()); $item = $items->first() ?: $items->appendItem(); $form['settings'] += $item->storageSettingsForm($form, $form_state, $this->entity->hasData()); // Build the configurable field values. $cardinality = $this->entity->getCardinality(); $form['cardinality_container'] = array('#parents' => array(), '#type' => 'fieldset', '#title' => $this->t('Allowed number of values'), '#attributes' => array('class' => array('container-inline', 'fieldgroup', 'form-composite'))); $form['cardinality_container']['cardinality'] = array('#type' => 'select', '#title' => $this->t('Allowed number of values'), '#title_display' => 'invisible', '#options' => array('number' => $this->t('Limited'), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED => $this->t('Unlimited')), '#default_value' => $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED : 'number'); $form['cardinality_container']['cardinality_number'] = array('#type' => 'number', '#default_value' => $cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? $cardinality : 1, '#min' => 1, '#title' => $this->t('Limit'), '#title_display' => 'invisible', '#size' => 2, '#states' => array('visible' => array(':input[name="cardinality"]' => array('value' => 'number')), 'disabled' => array(':input[name="cardinality"]' => array('value' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)))); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, FieldConfigInterface $field_config = NULL) { $this->field = $field_config; $form_state->set('field', $field_config); $bundle = $this->field->bundle; $entity_type = $this->field->entity_type; $field_storage = $this->field->getFieldStorageDefinition(); $bundles = entity_get_bundles(); $form_title = $this->t('%field settings for %bundle', array('%field' => $this->field->getLabel(), '%bundle' => $bundles[$entity_type][$bundle]['label'])); $form['#title'] = $form_title; $form['#field'] = $field_storage; // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->field->entity_type, 'bundle' => $this->field->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $items = $form['#entity']->get($this->field->getName()); $item = $items->first() ?: $items->appendItem(); if (!empty($field_storage->locked)) { $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->field->getLabel()))); return $form; } // Create a form structure for the field values. $form['field'] = array('#tree' => TRUE); // Build the non-configurable field values. $form['field']['field_name'] = array('#type' => 'value', '#value' => $this->field->getName()); $form['field']['entity_type'] = array('#type' => 'value', '#value' => $entity_type); $form['field']['bundle'] = array('#type' => 'value', '#value' => $bundle); // Build the configurable field values. $form['field']['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $this->field->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20); $form['field']['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->field->getDescription(), '#rows' => 5, '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => $this->displayAllowedTags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10); $form['field']['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->field->isRequired(), '#weight' => -5); // Add field settings for the field type and a container for third party // settings that modules can add to via hook_form_FORM_ID_alter(). $form['field']['settings'] = $item->fieldSettingsForm($form, $form_state); $form['field']['settings']['#weight'] = 10; $form['field']['third_party_settings'] = array(); $form['field']['third_party_settings']['#weight'] = 11; // Add handling for default value. if ($element = $items->defaultValuesForm($form, $form_state)) { $element = array_merge($element, array('#type' => 'details', '#title' => $this->t('Default value'), '#open' => TRUE, '#description' => $this->t('The default value for this field, used when creating new content.'))); $form['field']['default_value'] = $element; } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save settings'), '#button_type' => 'primary'); $form['actions']['delete'] = array('#type' => 'submit', '#value' => $this->t('Delete field'), '#submit' => array('::delete')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, FieldInstanceConfigInterface $field_instance_config = NULL) { $this->instance = $form_state['instance'] = $field_instance_config; $bundle = $this->instance->bundle; $entity_type = $this->instance->entity_type; $field_storage = $this->instance->getFieldStorageDefinition(); $bundles = entity_get_bundles(); $form_title = $this->t('%instance settings for %bundle', array('%instance' => $this->instance->getLabel(), '%bundle' => $bundles[$entity_type][$bundle]['label'])); $form['#title'] = $form_title; $form['#field'] = $field_storage; // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->instance->entity_type, 'bundle' => $this->instance->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $items = $form['#entity']->get($this->instance->getName()); if (!empty($field_storage->locked)) { $form['locked'] = array('#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->instance->getLabel()))); return $form; } // Create a form structure for the instance values. $form['instance'] = array('#tree' => TRUE); // Build the non-configurable instance values. $form['instance']['field_name'] = array('#type' => 'value', '#value' => $this->instance->getName()); $form['instance']['entity_type'] = array('#type' => 'value', '#value' => $entity_type); $form['instance']['bundle'] = array('#type' => 'value', '#value' => $bundle); // Build the configurable instance values. $form['instance']['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $this->instance->getLabel() ?: $field_storage->getName(), '#required' => TRUE, '#weight' => -20); $form['instance']['description'] = array('#type' => 'textarea', '#title' => $this->t('Help text'), '#default_value' => $this->instance->getDescription(), '#rows' => 5, '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '<br />' . $this->t('This field supports tokens.'), '#weight' => -10); $form['instance']['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $this->instance->isRequired(), '#weight' => -5); // Add instance settings for the field type. $form['instance']['settings'] = $items[0]->instanceSettingsForm($form, $form_state); $form['instance']['settings']['#weight'] = 10; // Add handling for default value. if ($element = $items->defaultValuesForm($form, $form_state)) { $element += array('#type' => 'details', '#title' => $this->t('Default value'), '#open' => TRUE, '#description' => $this->t('The default value for this field, used when creating new content.')); $form['instance']['default_value'] = $element; } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save settings')); $form['actions']['delete'] = array('#type' => 'submit', '#value' => $this->t('Delete field'), '#submit' => array(array($this, 'delete'))); return $form; }
/** * {@inheritdoc} */ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size) { // Check whether the whole field storage definition is gone, or just some // bundle fields. $storage_definition = $field_definition->getFieldStorageDefinition(); $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); $table_mapping = $this->getTableMapping(); $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted); // Get the entities which we want to purge first. $entity_query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC)); $or = $entity_query->orConditionGroup(); foreach ($storage_definition->getColumns() as $column_name => $data) { $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $column_name)); } $entity_query->distinct(TRUE)->fields('t', array('entity_id'))->condition('bundle', $field_definition->getTargetBundle())->range(0, $batch_size); // Create a map of field data table column names to field column names. $column_map = array(); foreach ($storage_definition->getColumns() as $column_name => $data) { $column_map[$table_mapping->getFieldColumnName($storage_definition, $column_name)] = $column_name; } $entities = array(); $items_by_entity = array(); foreach ($entity_query->execute() as $row) { $item_query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC))->fields('t')->condition('entity_id', $row['entity_id'])->orderBy('delta'); foreach ($item_query->execute() as $item_row) { if (!isset($entities[$item_row['revision_id']])) { // Create entity with the right revision id and entity id combination. $item_row['entity_type'] = $this->entityTypeId; // @todo: Replace this by an entity object created via an entity // factory, see https://www.drupal.org/node/1867228. $entities[$item_row['revision_id']] = _field_create_entity_from_ids((object) $item_row); } $item = array(); foreach ($column_map as $db_column => $field_column) { $item[$field_column] = $item_row[$db_column]; } $items_by_entity[$item_row['revision_id']][] = $item; } } // Create field item objects and return. foreach ($items_by_entity as $revision_id => $values) { $entity_adapter = $entities[$revision_id]->getTypedData(); $items_by_entity[$revision_id] = \Drupal::typedDataManager()->create($field_definition, $values, $field_definition->getName(), $entity_adapter); } return $items_by_entity; }
/** * Create the default value elements for a field. * * * @param $field_selected * * @return array */ protected function createDefaultValueElements($field_selected, FormStateInterface $form_state) { $elements = []; // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array( 'entity_type' => $this->getCurrentEntityType($form_state), 'bundle' => $this->getDefaultBundle($field_selected, $form_state), 'entity_id' => NULL ); $form['#entity'] = _field_create_entity_from_ids($ids); /** @var FieldItemListInterface $items */ $items = $form['#entity']->get($field_selected); $definition = $items->getFieldDefinition(); if ($this->isDefaultCompatible($definition)) { $item = $items->first() ?: $items->appendItem(); if ($widget_override = $this->getWidgetOverride($definition)) { $form_state->set('default_value_widget', $widget_override); } // Add handling for default value. if ($elements = $items->defaultValuesForm($form, $form_state)) { $elements = array_merge($elements , array( '#type' => 'details', '#title' => $this->t('Default value and Date Only Updates'), '#open' => TRUE, '#tree' => TRUE, '#description' => $this->t('The default value for this field, used when creating an update.'), )); $elements['#title'] = $this->t('Default value and Date Only Updates'); $elements['_no_form_display'] = [ '#type' => 'checkbox', '#title' => $this->t('Hide this field for an <strong>date only</strong> update.'), '#description' => $this->t('Hiding fields with a default value is very useful for creating updates where the user only has to enter an update date.' . ' ' . 'For example creating a "Publish Date" update type where user simply has to pick a date they would like the content published on.' ), ]; } } return $elements; }