public function MappingCreator::generateMapping in GatherContent 8.5
Generates template, mapping and migration definition for given entity type and bundle.
Parameters
string $entityTypeId: Entity type.
string $bundle: Bundle.
string $projectId: Project ID.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\Core\Entity\EntityStorageException
File
- gathercontent_upload/
src/ Export/ MappingCreator.php, line 181
Class
- MappingCreator
- Class for handling import/update logic from GatherContent to Drupal.
Namespace
Drupal\gathercontent_upload\ExportCode
public function generateMapping(string $entityTypeId, string $bundle, string $projectId) {
$groups = [];
$mappingData = [];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entityTypeId);
$templateName = ucfirst($bundle);
if (!empty($bundles[$bundle]['label'])) {
$templateName = $bundles[$bundle]['label'];
}
$fields = $this->entityFieldManager
->getFieldDefinitions($entityTypeId, $bundle);
$languages = [
$this->languageManager
->getDefaultLanguage(),
];
if (isset($this->contentTranslation) && $this->contentTranslation
->isEnabled($entityTypeId, $bundle)) {
$languages = $this->languageManager
->getLanguages();
}
foreach ($languages as $language) {
$groupUuid = $this->uuidService
->generate();
$group = [
'uuid' => $groupUuid,
'name' => $language
->getName(),
'fields' => [],
];
$mappingData[$groupUuid] = [
'type' => 'content',
'language' => $language
->getId(),
'elements' => [],
];
$formDisplay = $this->entityDisplayRepository
->getFormDisplay($entityTypeId, $bundle);
$components = $formDisplay
->getComponents();
if (!empty($components)) {
$fieldsOrderedByWeight = [];
foreach ($components as $componentName => $componentValue) {
$fieldsOrderedByWeight[$componentValue['weight']] = $componentName;
}
krsort($fieldsOrderedByWeight);
foreach ($fieldsOrderedByWeight as $fieldName) {
if (array_key_exists($fieldName, $fields)) {
$fields = [
$fieldName => $fields[$fieldName],
] + $fields;
}
}
}
$this
->processFields($fields, $language, $group, $mappingData, $groupUuid);
$groups[] = $group;
}
$template = $this->client
->templatePost($projectId, $templateName, new Structure([
'uuid' => $this->uuidService
->generate(),
'groups' => $groups,
]));
$this->client
->templateGet($template->id);
$templateData = serialize($this->client
->getBody(TRUE));
/** @var \Drupal\gathercontent\Entity\Mapping $mapping */
$mapping = Mapping::create([
'id' => $template->id,
'gathercontent_project_id' => $projectId,
'gathercontent_project' => $this
->getProjectName($projectId),
'gathercontent_template_id' => $template->id,
'gathercontent_template' => $templateName,
'template' => $templateData,
]);
$mapping
->setMappedEntityType($entityTypeId);
$mapping
->setContentType($bundle);
$mapping
->setContentTypeName($templateName);
$mapping
->setData(serialize($mappingData));
$mapping
->setUpdatedDrupal(time());
$mapping
->save();
if (!empty($mappingData)) {
$this->migrationDefinitionCreator
->setMapping($mapping)
->setMappingData($mappingData)
->createMigrationDefinition();
}
}