Problem/Motivation

Is there any way to import the attachments as Media if the module from Drupal core is enabled?
The files interface in Drupal is very bad and worpress interface for the images are Media as well, it would make sense to import them as media as well in Drupal.

Steps to reproduce

Proposed resolution

Add a step for the attachment to pick if you want to import them as images or media and configure it.

Remaining tasks

Support other types of media based on the extension of the files.

User interface changes

Add an extra step with the selection about the attachments. I also made the path for the files configurable instead of being fixed to featured_image.

API changes

Data model changes

See also #3523483: import featured image as entity reference for entity reference field imports.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

phjou created an issue. See original summary.

phjou’s picture

Issue summary: View changes
StatusFileSize
new12.16 KB

Ok first patch with support for media fields for the article image, and import the images as media.

The patch comes on top of lots of other issues, mixing the patches is a nightmare so the patch has to be applied after at least this patch:
#3173973: Pluggable extension, support contrib + custom WordPress, Yoast SEO

phjou’s picture

I have a fork of the module since I need so many patches to this module if it helps seeing what has been modified:
https://github.com/pjoulot/wordpress_migrate

phjou’s picture

Ok I got a new patch which is pretty nice now and can start being reviewed

- Support for Audio and Document
- Allow to choose which media type you want
- The field for importing is automatically detected based on the media type you chose (MediaSource)
- You can choose the file extensions you want to match in order to import your media. The extension are automatically filled in the input in AJAX based on the extension that the field of the chosen media type supports.

The patch is still based on an advanced version of the module, you need to apply the patches listed in comment #2.

Screenshot of the interface:

Screenshot of the attachment form

phjou’s picture

StatusFileSize
new1.13 KB
new23.43 KB

New patch to fix a problem when the user decide to not use Media.

phjou’s picture

Status: Active » Needs review
phjou’s picture

StatusFileSize
new1.79 KB
new0 bytes

Just improved the patch:

- Use the default location input
- Add support for the import of the alt of the image

phjou’s picture

StatusFileSize
new24.13 KB

Oups empty file, sorry.

phjou’s picture

irina matusevich’s picture

Updated the patch according to the current version of the module

linhnm’s picture

StatusFileSize
new29.04 KB
new12.11 KB

Rerolled patch from #10

hongpong’s picture

Priority: Normal » Major

Can anyone else check this one out and give it RTBC? Thank you!

bserem’s picture

None of the latest patches apply anymore. Trying to apply them by hand ends in deprecation warnings.

hongpong’s picture

I added a few type safety updates flagged in the IDE. Otherwise should be a clean re-roll against today's 8.x-3.x. i saw the WordPressMigrationGenerator "path alias generator" part, I think the reroll patch here is correct but should be double checked.

(the MR !21 is the same as this patchfile.)

I applied it using patch -p1 < 3173916-11.patch from the base dir of the module as working directory. This helps deal with stuck hunk patch application. I did not test this but wanted to give people a clean spot to start again with this issue.

hongpong’s picture

StatusFileSize
new30.2 KB

Oops - my local 8.x-3.x was missing the latest "path alias generator" commit. re rolled again.

hongpong’s picture

some more notices. Per !MR21 reroll . https://git.drupalcode.org/project/wordpress_migrate/-/merge_requests/21

Major - Access to an undefined property Drupal\wordpress_migrate\WordPressMigrationGenerator::$mediaAudioID.
in src/WordPressMigrationGenerator.php:167

Major - Access to an undefined property Drupal\wordpress_migrate\WordPressMigrationGenerator::$mediaDocumentID.
in src/WordPressMigrationGenerator.php:167

Major - Access to an undefined property Drupal\wordpress_migrate\WordPressMigrationGenerator::$mediaImageID.
in src/WordPressMigrationGenerator.php:167

And additional phpstan and phpcs notices.

hongpong’s picture

Status: Needs review » Needs work

Some upstream changes in wordpress_migrate_ui/src/Form/ImageSelectForm.php - the merge conflict is only there, basically got similar modernization by other folks. I'm not actively going to tangle with this one more for now, but if someone else wants to hammer away on it, go ahead.

abarrio made their first commit to this issue’s fork.

abarrio’s picture

Status: Needs work » Needs review

Created a new MR with branch 3173916-import-as-media rebased to 8.x-3.x.

hongpong’s picture

Thanks so much abarrio for rebasing. Any ideas what we can do to fix the "Access to an undefined property" stuff flagged above? That seems like the biggest flagged snag currently.

abarrio’s picture

Uploaded a fixing for the undefined property error and fixing some other coding standards on WordPressMigrationGenerator.

abarrio’s picture

Uploaded one fix for an undefined index url error when executing drush ms and changed xpath expressions on media migrations with a simpler one.

hongpong’s picture

excellent stuff abarrio. do you think it is RTBC yet? I don't have a test xml with audio attachment to test.

abarrio’s picture

No, I think there is more work remaining to have it as RTBC.
I think we still need to work on:

  • Test each media type to check that is completely working automatically without changing yml
  • Fix coding standard of all new code
  • Maybe be able to check if xml file is completely correct to avoid problems when importing (Maybe could be done on a follow up issue)
  • Create some unit tests
  • Fix all problems that appears on merges

I think with this points resolved we can proceed with this as RTBC.

abarrio’s picture

Status: Needs review » Needs work
hongpong’s picture

regarding checking the xml file see #3248042: Add XML Lint to check the WordPress exports xml lint could be added to composer but i figured that would intensify requirements. as well as #3348775: WP import xml entities & security considerations for a note on other considerations.

regarding testing media types I suppose this would require a new test site altogether with the types of assets in question.

if you think we can subdivide out any portion of the patch as well (in other words if you think some standalone portion would actually be RTBC as it is), let me know. Thanks for all this!

abarrio’s picture

Makes sense to me.

Maybe we can merge and complete this issue as a first approach and then continue working on some other issues to finish this big development part.

I think we need a meta issue with a complete list of tasks needed to finish it. If I have time this days I'll create it.

abarrio’s picture

Status: Needs work » Needs review
hongpong’s picture

one small issue, unless i'm mistaken there are duplicated migration yml files in the root dir of the project on the Merge request?

abarrio’s picture

Status: Needs review » Needs work

that's completely true!!
The only yml files that needs to be there are the ones from migrations folder.

abarrio’s picture

Removed unnecessary yml files from MR.
As I said on #26 I think we should work on those points but maybe we can proceed with this as a first approach and them complete the development in a follow up issue.

hongpong’s picture

Regardless of the 'nice to have' like xml check, unit tests and certain code standard flags, I think we need to be assured that the migration is working correctly for most use cases.
Forgive my silly question but is "document" like PDF files and things of that nature? In other words if someone has added a plugin that allows the PDF uploads in the media library, this would cover importing those. I'm a little more familiar with the built in audio content type.
[In late Jan or early Feb i'm going to take at least a couple days to focus on this module again -- which would involve composing a new site in WP with these content types and trying to run the migrate in the patch. we could roll in before then with more clear test results but i am down to make that effort a little later.]

abarrio’s picture

StatusFileSize
new57.6 KB

Actually on each media is getting the default type and checking the extensions allowed there to get default value when configuring migration:

File extensions

it is not different between types of attachment files, it is just getting them to drupal files and then converting it into media.

norman.lol’s picture

StatusFileSize
new46.25 KB

Need a static patch of the current state of MR 35. Thank you for your work so far 🤗

wotts’s picture

Great! I did have to change the selector syntax from:

item_selector: '/rss/channel/item[wp:post_type="attachment" and wp:attachment_url[contains(text(), "gif") or wp:attachment_url[contains(text(), "jpg") or wp:attachment_url[contains(text(), "jpeg"))]'

To:

item_selector: '/rss/channel/item[wp:post_type="attachment" and (contains(wp:attachment_url, "png") or contains(wp:attachment_url, "jpg") or contains(wp:attachment_url, "jpeg"))]'
hongpong’s picture

@wotts can you say where you changed this? was it in wordpress_attachments.yml at :

source:
  plugin: url
  data_fetcher_plugin: file
  data_parser_plugin: xml
  urls: []
  item_selector: '/rss/channel/item[wp:post_type="attachment"]'

Thank you.

hongpong’s picture

I muddled things and needed to put in a rebase on latest 8.x-3.x HEAD after Drupal 11 support added and refactored WordPressMigrationGenerator. going to refactor a bit to make these functions smaller. Pulled the previous MR into MR39: https://git.drupalcode.org/project/wordpress_migrate/-/merge_requests/39

Re upping what abarrio suggested if anyone wants to get in:

  • Test each media type to check that is completely working automatically without changing yml
  • Fix coding standard of all new code
  • Maybe be able to check if xml file is completely correct to avoid problems when importing in recent days ( I started a new module to cover this - https://gitlab.com/HongPong/xml_vuln_detector ) - not needed to do this now imo
  • Create some unit tests
  • Fix all problems that appears on merges
hongpong’s picture

K I tried to extract the media field loop however I think I missed the mark. I also extracted the MediaTypes array to the properties of the class.

Warning: Undefined array key "plugin" in Drupal\migrate\Plugin\Migration->findMigrationDependencies() (line 685 of core/modules/migrate/src/Plugin/Migration.php). (and line 688). Appears after specifying all the fields at urls like - : /admin/structure/migrate/manage/my_wp_firstmedia1/migrations - the imports are "no data found".

Drupal\migrate\Plugin\Migration->findMigrationDependencies() (Line: 655)
Drupal\migrate\Plugin\Migration->getMigrationDependencies() (Line: 124)
Drupal\migrate\Plugin\MigrationPluginManager->createInstances() (Line: 93)
Drupal\migrate\Plugin\MigrationPluginManager->createInstance() (Line: 132)
Drupal\migrate_tools\Controller\MigrationListBuilder->buildRow() (Line: 272)
Drupal\Core\Entity\EntityListBuilder->render() (Line: 23)
Drupal\Core\Entity\Controller\EntityListController->listing()
call_user_func_array() (Line: 123)

On Drupal 11.1.5. Tried to extract as.

foreach ($media_types as $media_type_id => $media_type) {
        $config_key = $media_type_id . '_media_type';
        if ($this->configuration[$config_key]) {
          $this->CreateSingleMediaTypeMigration($media_type['migration_id'], $media_type_id, $process, $config_key);
        }
      }

and new function

  public function CreateSingleMediaTypeMigration($migration_id, string $media_type_id, $process, string $config_key): void {
    $this->migrateInfo[$migration_id] = $this->configuration['prefix'] . 'wordpress_media_' . $media_type_id;
    $migration = $this->createEntityFromPlugin('wordpress_media_' . $media_type_id, $this->migrateInfo[$migration_id]);
    $migration->set('migration_group', $this->configuration['group_id']);
   ...
    $migration->save();
    unset($process, $migration);

Added the unset, trying to make a more clean function.

hongpong’s picture

abarrio’s picture

@hongpong Which is the status of merge? It needs more work to do or just revision?

hongpong’s picture

@abarrio I haven't had the time / ability to move this along further in recent months. Hoping people can test.