[6.x] Feature/import - #19612
Draft
i-just wants to merge 227 commits into
Draft
[6.x] Feature/import#19612i-just wants to merge 227 commits into
i-just wants to merge 227 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It imports data from JSON, CSV or XML files into Craft CMS elements or Eloquent models.
Core concepts:
Extensibility:
CraftCms\Cms\Import\Commands\ImportData Types:
RegisterDataTypeseventTransformers:
-
ElementTransformer=> used by default by all elements-
EntryTransformer=> default when importing Entries-
AssetTransformer=> default when importing AssetsElementTransformerauto-matches incoming keys to attributes and field handles even when the names aren’t exact (e.g.Plain Text/plain_text→plainText)myPlainTextField, or run complex checks to decide whatvalue
myOtherFieldgetsImporters:
-
ElementImporter(abstract) =>EntryImporter,AssetImporter,UserImporter. Each implementstargetClass(). Common settings:site(required) andfieldLayout-
EntryImporteraddssectionandentryType-
AssetImporteraddsvolume-
ModelImporter(abstract) =>SystemMessageImporteris the only built-in.Addresshas no importer, so it’s never a standalone target. It can only be imported as nested content (Addresses field / User addresses)0disables batching)RegisterImporterTypeseventImport plans:
- editable => created via the Control Panel and stored in the database (
import_plans)- non-editable => file-based, stored in
config/craft/import.phpand committed to your repository. Invalid file-based plans are skipped and logged- choose the importer and fill in its settings. For example, for an Entry importer, you pick the site, section and entry type
- specify the file that holds the data you want to import
- optionally choose a transformer and a batch size
- define which importable attribute and field maps to which incoming value (
map)- choose which values should be used to match against existing elements (
matchCriteria)- choose which values should be cleared out if they’re empty or missing from the incoming data (
clearableItems)- choose whether you’d like to keep nested content that’s missing from the incoming data (
keepMissingNestedElements)Mapping UI
canBeSet: false(e.g.id,uid) can only be used for matching, not setMatch criteria — three tiers, last wins
matchCriteriakey on the row,[systemHandle => incomingKey].Also looks inside
$data['fields'].additionalMatchCriteria(), already resolved to actual values.Clearable items
An array shaped like the match criteria. If a field or attribute is
nulland explicitly appliedunset()entirely, so the existing value is left untouched.Importing:
-
artisan craft:import:entry {file} [--site=] [--section=] [--entryType=] [--transformer=] [--matchCriteria=]-
artisan craft:import:asset {file} [--site=] [--volume=] …-
artisan craft:import:user {file} …-
artisan craft:import:system-message {file} …- any options you leave out are prompted for
Making things importable:
ElementImporter/ModelImportersubclass viaRegisterImporterTypesCraftCms\Cms\Support\Attributes\Importablemarks an element’s properties as importable. Its flags areexcludeFromUiMapping,isContainer,canBeMatchCriteria,canBeClearedandcanBeSetCraftCms\Cms\FieldLayout\Contracts\ImportableFieldLayoutElementInterfacemakes field layout elements importableCraftCms\Cms\Field\Contracts\ImportableElementContainerFieldInterfacelets container fields be imported correctlyEvents:
RegisterDataTypes,RegisterImporterTypes,DataImporting/DataImported,ImportPlanSaving/ImportPlanSaved,ImportDispatching/ImportDispatched. The*ingevents are cancellable.Permissions:
There’s a permissions group
importwith the following permissions:viewImportPlans,saveImportPlans,deleteImportPlans,triggerImportPlans.Related issues
CMS-2390