/** * Create field elements for all field on the entity type to update. * * @param \Drupal\Core\Form\FormStateInterface $form_state * * @return array */ protected function createFieldsElements(FormStateInterface $form_state) { $base_options = []; /** @var FieldStorageDefinitionInterface[] $config_fields */ $config_fields = []; $entity_type = $this->entity->getUpdateEntityType(); $target_entity_label = $this->targetTypeLabel($this->entity); $target_bundle_label = $this->targetTypeBundleLabel($this->entity); $elements = ['#type' => 'container']; $destination_fields = $this->getDestinationFields(); $map = $this->entity->getFieldMap(); foreach ($destination_fields as $destination_field) { $field_name = $destination_field->getName(); if (!in_array($field_name, $map)) { if ($destination_field->isBaseField()) { $base_options[$field_name] = $destination_field->getLabel(); } else { $config_fields[] = $destination_field; } } } $elements['base_fields'] = ['#type' => 'checkboxes', '#title' => $this->t('Base Fields'), '#options' => $base_options, '#description' => $this->t('These fields are available on all @label entities and may not be configurable.', ['@label' => $target_entity_label])]; $elements['config_fields'] = ['#tree' => TRUE, '#type' => 'fieldset', '#title' => $this->t('Configurable Fields')]; if ($this->targetSupportBundles($this->entity)) { $elements['config_fields']['#description'] = $this->t('These fields have been added to different @bundle_label bundles of @entity_label. They may not be on all @entity_label entities.', ['@entity_label' => $target_entity_label, '@bundle_label' => $target_bundle_label]); } else { $elements['config_fields']['#description'] = $this->t('These fields have been added to @entity_label entities.', ['@entity_label' => $target_entity_label]); } foreach ($config_fields as $config_field) { $instances = $this->fieldManager->getAllFieldConfigsForField($config_field, $entity_type); if ($instances) { $instance_options = ['' => $this->t('(Don\'t clone)')]; foreach ($instances as $bundle => $instance) { $instance_options[$instance->id()] = $this->t('As it is configured in @bundle as @label', ['@bundle' => $bundle, '@label' => $instance->getLabel()]); } $elements['config_fields'][$config_field->getName()] = ['#type' => 'select', '#title' => $config_field->getLabel(), '#options' => $instance_options]; } } //$this->entity_field_manager->getFieldDefinitions(); return $elements; }
protected function targetSupportBundles(ScheduledUpdateTypeInterface $scheduledUpdateType) { return $this->typeSupportsBundles($scheduledUpdateType->getUpdateEntityType()); }
/** * Get the entity definition for the entity to be updated. * * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduledUpdateType * * @return array|\Drupal\Core\Entity\EntityTypeInterface|null * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ protected function getUpdateTypeDefinition(ScheduledUpdateTypeInterface $scheduledUpdateType) { if ($update_entity_type = $scheduledUpdateType->getUpdateEntityType()) { return $this->entityTypeManager->getDefinition($update_entity_type); } return NULL; }
/** * {@inheritdoc} */ public function createNewReferenceField(array $new_field_settings, ScheduledUpdateTypeInterface $scheduled_update_type) { $entity_type = $scheduled_update_type->getUpdateEntityType(); $field_name = $new_field_settings['field_name']; $label = $new_field_settings['label']; if ($new_field_settings['cardinality'] == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { $new_field_settings['cardinality_number'] = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED; } $field_storage_values = ['field_name' => $field_name, 'entity_type' => $entity_type, 'type' => 'entity_reference', 'translatable' => FALSE, 'settings' => ['target_type' => 'scheduled_update'], 'cardinality' => $new_field_settings['cardinality_number']]; FieldStorageConfig::create($field_storage_values)->save(); $bundles = array_filter($new_field_settings['bundles']); foreach ($bundles as $bundle) { $field_values = ['field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $label, 'translatable' => FALSE, 'settings' => ['handler_settings' => ['target_bundles' => [$scheduled_update_type->id()]]]]; $field = FieldConfig::create($field_values); $field->save(); /** @var EntityFormDisplay $formDisplay */ $formDisplay = EntityFormDisplay::load("{$entity_type}.{$bundle}.default"); $form_options = ['type' => 'inline_entity_form_complex', 'weight' => '11', 'settings' => ['override_labels' => TRUE, 'label_singular' => $label, 'label_plural' => $label . 's', 'allow_new' => TRUE, 'match_operator' => 'CONTAINS', 'allow_existing' => FALSE]]; $formDisplay->setComponent($field_name, $form_options); $formDisplay->save(); } }
/** * Create a form display for a newly clone field. * * This function attempts to use same setting settings as the source field. * * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduled_update_type * @param $field_name * @param $field_config_id * @param $entity_type * @param $definition * Source field definition * @param $new_field_name */ protected function createFormDisplay(ScheduledUpdateTypeInterface $scheduled_update_type, $field_config_id, FieldStorageDefinitionInterface $definition, $new_field_name) { $destination_bundle = $scheduled_update_type->id(); $field_name = $definition->getName(); $entity_type = $scheduled_update_type->getUpdateEntityType(); /** @var EntityFormDisplay $destination_form_display */ $destination_form_display = EntityFormDisplay::load("scheduled_update.$destination_bundle.default"); if (empty($destination_form_display)) { $destination_form_display = EntityFormDisplay::create([ 'targetEntityType' => 'scheduled_update', 'bundle' => $destination_bundle, 'mode' => 'default', 'status' => TRUE, ]); } $display_options = []; if ($field_config_id) { $parts = explode('.', $field_config_id); $source_bundle = $parts[1]; /** @var EntityFormDisplay $source_form_display */ $source_form_display = EntityFormDisplay::load("$entity_type.$source_bundle.default"); $display_options = $source_form_display->getComponent($field_name); } else { if ($definition instanceof BaseFieldDefinition) { $display_options = $definition->getDisplayOptions('form'); if (empty($display_options)) { if ($definition->getType()) { // Provide default display for base boolean fields that don't have their own form display $display_options = [ 'type' => 'boolean_checkbox', 'settings' => ['display_label' => TRUE], ]; } } } } if (empty($display_options)) { $display_options = []; } if ($destination_form_display) { $destination_form_display->setComponent($new_field_name, $display_options); $destination_form_display->save(); } else { // Alert user if display options could not be created. // @todo Create default display options even none on source. drupal_set_message( $this->t( 'Form display options could not be created for @field they will have to be created manually.', ['@field' => $field_name] ), 'warning'); } }