/** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $id_parts = explode("/", $value); $entity = $this->entityTypeManager->getDefinition($id_parts[0], FALSE); if ($entity == NULL) { // Or maybe migrate comments some other way. throw new MigrateSkipRowException(); } return $id_parts[0]; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Get the custom entity ID. $custom_entity_id = $this->entity->getEntityType()->getBundleOf(); // Get the bundle key for the custom entity type. $custom_entity_bundle_key = $this->entityManager->getDefinition($custom_entity_id)->getKey('bundle'); $entities = $this->queryFactory->get($custom_entity_id)->condition($custom_entity_bundle_key, $this->entity->id())->execute(); if (!empty($entities)) { $caption = '<p>' . $this->formatPlural(count($entities), '%label is used by 1 entity on your site. You can not remove this entity type until you have removed all of the %label entities.', '%label is used by @count entities on your site. You may not remove %label until you have removed all of the %label entities.', ['%label' => $this->entity->label()]) . '</p>'; $form['description'] = array('#markup' => $caption); return $form; } else { return parent::buildForm($form, $form_state); } }
protected function addJoin($type, $table, $join_condition, $langcode) { $arguments = array(); if ($langcode) { $entity_type_id = $this->sqlQuery->getMetaData('entity_type'); $entity_type = $this->entityManager->getDefinition($entity_type_id); // Only the data table follows the entity language key, dedicated field // tables have an hard-coded 'langcode' column. $langcode_key = $entity_type->getDataTable() == $table ? $entity_type->getKey('langcode') : 'langcode'; $placeholder = ':langcode' . $this->sqlQuery->nextPlaceholder(); $join_condition .= ' AND %alias.' . $langcode_key . ' = ' . $placeholder; $arguments[$placeholder] = $langcode; } return $this->sqlQuery->addJoin($type, $table, NULL, $join_condition, $arguments); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $config_types = $this->getConfigTypes(); $config_name = $input->getArgument('config-name'); if (!$config_name) { $config_type = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-type'), array_keys($config_types), $this->trans('commands.config.export.single.options.simple-configuration')); $config_names = $this->getConfigNames($config_type); $config_name = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-name'), array_keys($config_names)); if ($config_type !== 'system.simple') { $definition = $this->entityManager->getDefinition($config_type); $config_name = $definition->getConfigPrefix() . '.' . $config_name; } $input->setArgument('config-name', $config_name); } }
/** * {@inheritdoc} */ public function addField($field, $type, $langcode) { $entity_type_id = $this->sqlQuery->getMetaData('entity_type'); $age = $this->sqlQuery->getMetaData('age'); // This variable ensures grouping works correctly. For example: // ->condition('tags', 2, '>') // ->condition('tags', 20, '<') // ->condition('node_reference.nid.entity.tags', 2) // The first two should use the same table but the last one needs to be a // new table. So for the first two, the table array index will be 'tags' // while the third will be 'node_reference.nid.tags'. $index_prefix = ''; $specifiers = explode('.', $field); $base_table = 'base_table'; $count = count($specifiers) - 1; // This will contain the definitions of the last specifier seen by the // system. $propertyDefinitions = array(); $entity_type = $this->entityManager->getDefinition($entity_type_id); $field_storage_definitions = array(); // @todo Needed for menu links, make this implementation content entity // specific after https://drupal.org/node/2256521. if ($entity_type instanceof ContentEntityTypeInterface) { $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); } for ($key = 0; $key <= $count; $key++) { // If there is revision support and only the current revision is being // queried then use the revision id. Otherwise, the entity id will do. if (($revision_key = $entity_type->getKey('revision')) && $age == EntityStorageInterface::FIELD_LOAD_CURRENT) { // This contains the relevant SQL field to be used when joining entity // tables. $entity_id_field = $revision_key; // This contains the relevant SQL field to be used when joining field // tables. $field_id_field = 'revision_id'; } else { $entity_id_field = $entity_type->getKey('id'); $field_id_field = 'entity_id'; } // This can either be the name of an entity base field or a configurable // field. $specifier = $specifiers[$key]; if (isset($field_storage_definitions[$specifier])) { $field_storage = $field_storage_definitions[$specifier]; } else { $field_storage = FALSE; } // If we managed to retrieve a configurable field, process it. if ($field_storage instanceof FieldStorageConfigInterface) { // Find the field column. $column = $field_storage->getMainPropertyName(); if ($key < $count) { $next = $specifiers[$key + 1]; // Is this a field column? $columns = $field_storage->getColumns(); if (isset($columns[$next]) || in_array($next, FieldStorageConfig::getReservedColumns())) { // Use it. $column = $next; // Do not process it again. $key++; } // If there are more specifiers, the next one must be a // relationship. Either the field name followed by a relationship // specifier, for example $node->field_image->entity. Or a field // column followed by a relationship specifier, for example // $node->field_image->fid->entity. In both cases, prepare the // property definitions for the relationship. In the first case, // also use the property definitions for column. if ($key < $count) { $relationship_specifier = $specifiers[$key + 1]; $propertyDefinitions = $field_storage->getPropertyDefinitions(); // Prepare the next index prefix. $next_index_prefix = "{$relationship_specifier}.{$column}"; } } $table = $this->ensureFieldTable($index_prefix, $field_storage, $type, $langcode, $base_table, $entity_id_field, $field_id_field); $sql_column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $column); } else { // 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 preferred, which is why // it gets added before the base table. $entity_tables = array(); if ($data_table = $entity_type->getDataTable()) { $this->sqlQuery->addMetaData('simple_query', FALSE); $entity_tables[$data_table] = $this->getTableMapping($data_table, $entity_type_id); } $entity_base_table = $entity_type->getBaseTable(); $entity_tables[$entity_base_table] = $this->getTableMapping($entity_base_table, $entity_type_id); $sql_column = $specifier; $table = $this->ensureEntityTable($index_prefix, $specifier, $type, $langcode, $base_table, $entity_id_field, $entity_tables); } // If there are more specifiers to come, it's a relationship. if ($field_storage && $key < $count) { // Computed fields have prepared their property definition already, do // it for properties as well. if (!$propertyDefinitions) { $propertyDefinitions = $field_storage->getPropertyDefinitions(); $relationship_specifier = $specifiers[$key + 1]; $next_index_prefix = $relationship_specifier; } // Check for a valid relationship. if (isset($propertyDefinitions[$relationship_specifier]) && $field_storage->getPropertyDefinition('entity')->getDataType() == 'entity_reference') { // If it is, use the entity type. $entity_type_id = $propertyDefinitions[$relationship_specifier]->getTargetDefinition()->getEntityTypeId(); $entity_type = $this->entityManager->getDefinition($entity_type_id); $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); // Add the new entity base table using the table and sql column. $join_condition = '%alias.' . $entity_type->getKey('id') . " = {$table}.{$sql_column}"; $base_table = $this->sqlQuery->leftJoin($entity_type->getBaseTable(), NULL, $join_condition); $propertyDefinitions = array(); $key++; $index_prefix .= "{$next_index_prefix}."; } else { throw new QueryException(format_string('Invalid specifier @next.', array('@next' => $relationship_specifier))); } } } return "{$table}.{$sql_column}"; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $error = FALSE; $values = $form_state->getValues(); $destinations = array(); $entity_type = $this->entityManager->getDefinition($this->entityTypeId); // Create new field. if ($values['new_storage_type']) { $field_storage_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'type' => $values['new_storage_type'], 'translatable' => $values['translatable']]; $field_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['label'], 'translatable' => FALSE]; $widget_id = $formatter_id = NULL; // Check if we're dealing with a preconfigured field. if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) { list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3); $field_storage_values['type'] = $field_type; $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class']; $field_options = $field_type_class::getPreconfiguredOptions()[$option_key]; // Merge in preconfigured field storage options. if (isset($field_options['field_storage_config'])) { foreach (array('cardinality', 'settings') as $key) { if (isset($field_options['field_storage_config'][$key])) { $field_storage_values[$key] = $field_options['field_storage_config'][$key]; } } } // Merge in preconfigured field options. if (isset($field_options['field_config'])) { foreach (array('required', 'settings') as $key) { if (isset($field_options['field_config'][$key])) { $field_values[$key] = $field_options['field_config'][$key]; } } } $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL; $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL; } // Create the field storage and field. try { $this->entityManager->getStorage('field_storage_config')->create($field_storage_values)->save(); $field = $this->entityManager->getStorage('field_config')->create($field_values); $field->save(); $this->configureEntityFormDisplay($values['field_name'], $widget_id); $this->configureEntityViewDisplay($values['field_name'], $formatter_id); // Always show the field settings step, as the cardinality needs to be // configured for new fields. $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters); // Store new field information for any additional submit handlers. $form_state->set(['fields_added', '_add_new_field'], $values['field_name']); } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error'); } } // Re-use existing field. if ($values['existing_storage_name']) { $field_name = $values['existing_storage_name']; try { $field = $this->entityManager->getStorage('field_config')->create(array('field_name' => $field_name, 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['existing_storage_label'])); $field->save(); $this->configureEntityFormDisplay($field_name); $this->configureEntityViewDisplay($field_name); $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters); // Store new field information for any additional submit handlers. $form_state->set(['fields_added', '_add_existing_field'], $field_name); } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error'); } } if ($destinations) { $destination = $this->getDestinationArray(); $destinations[] = $destination['destination']; $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state)); } elseif (!$error) { drupal_set_message($this->t('Your settings have been saved.')); } }