Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Metatag module currently doesn't have any integration for importing meta tags through the Feeds module. Initial patch to follow.
UPD: Patch expects that source string will be in JSON format.
E.g.:
{"title":"title value","description":"description value",...}
Comment | File | Size | Author |
---|---|---|---|
#44 | metatag_feeds_support_3011202.patch | 3.38 KB | djschoone |
#38 | Metatag_feeds_support.patch | 3.74 KB | someshver |
#30 | feeds-integration-3011202-30.patch | 2.93 KB | damondt |
|
Issue fork metatag-3011202
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
Comment #2
artematem CreditAttribution: artematem at FFW commentedPatch expects that source string will be in JSON format.
E.g.:
{"title":"title value","description":"description value",...}
Comment #3
artematem CreditAttribution: artematem at FFW commentedRemove no needed "return".
Comment #4
artematem CreditAttribution: artematem at FFW commentedComment #5
DamienMcKennaComment #6
DamienMcKennaThanks for doing this work!
Comment #7
DamienMcKennaIs there a better way of handling the input string rather than requiring it be a JSON-encoded string?
Comment #8
mitchems CreditAttribution: mitchems commentedIn the 7.X version of Feeds with Metatag module installed you could import to the individual metatags.
Any plans to allow for this again?
Comment #9
opsdemon CreditAttribution: opsdemon commentedHi @artematem,
I'm trying to set the "canonical_url" metatag via Feeds and came across this issue and your patch.
I've incorporated the patch successfully and set up a new mapping in Feeds for the "metatags" column to the "field_metatags" field.
I next tried to import an article with the value for the "metatags" field in the csv set to a test url:
{"canonical_url":"https://www.drupal.org/project/metatag/issues/3011202"}
When I do the import I can see that it has just used the default value ("[node:url]"), ignoring the value from the csv.
I've also tried setting the metatag values for "title" and "description" as in your example and these didn't work either.
Please can you let me know if there is anything else that needs to be done for a successful import of metatags using this patch?
Your help is much appreciated.
Thanks,
Gerald.
Comment #10
opsdemon CreditAttribution: opsdemon commentedPlease ignore my previous comment.
The issue with the canonical url metatag was just to use the id ("canonical_url") instead of the name ("canonical"). The issue with the title and description metatags not updating was just a compilation error on my part.
The patch is working fine now. @artematem, thanks for the code!
Comment #11
selva.swamy@gmail.com CreditAttribution: selva.swamy@gmail.com as a volunteer and commented@DamienMcKenna Tested with patch in comment#3 and it is working as expected. I think if we do not require any tests this patch can be marked as RTBC.
Since the raw value for metatag are exported as JSON when using module like https://www.drupal.org/project/raw_formatter, I think JSON is ok as we can import multiple metatag values using single mapping.
However a check if the input is indeed JSON would be good. If not, a error can be thrown.
Could you let us know your thoughts as we need this patch for a project?
Comment #12
DamienMcKennaI think at the very least the input needs some protection to ensure the input is valid JSON; the plugin's comment also needs improvement (typo, wording).
Comment #13
selva.swamy@gmail.com CreditAttribution: selva.swamy@gmail.com as a volunteer and commented@DamienMcKenna Updated patch attached. I also ran phpcs for this code and fixed some minor coding standard issues. Please have a look.
Comment #14
selva.swamy@gmail.com CreditAttribution: selva.swamy@gmail.com as a volunteer and commentedComment #15
selva.swamy@gmail.com CreditAttribution: selva.swamy@gmail.com as a volunteer and commented@DamienMcKenna Could you please review the patch in comment#13?
Comment #16
fbreckx CreditAttribution: fbreckx commentedI'm not sure on how I should format the JSON results. I have a list of blogitems as in the excerpt below.
'blogMetaTitle' and 'blogMetaDesc' is for the metadata.
I tried rewriting the result to get it in one field, but this doesn't seem right :)
{'title':'[title]','description':'[description]'}
"blogMetaDesc": "{'title':'7 tips om goed te slapen tijdens koude winternachten | Dorsoo','description':'Tijdens koude winternachten daalt de slaapkamertemperatuur. Dat kan zorgen voor een slechte nachtrust. Wat kan u doen om goed te slapen? Lees onze 7 tips. '}",
That results in an error that says the field can't contain more than one value.
What's the correct way?
Comment #17
DamienMcKennaI don't know what data structures Feeds works with in D8 so I can't help, sorry.
This is where some test coverage would help.
Comment #18
fbreckx CreditAttribution: fbreckx commented@artematem, could you help me out please? I can't seem to structure my data feed the right way ...
Comment #19
artematem CreditAttribution: artematem at FFW commented@fbreckx, I believe this is because your whole feed is in JSON format.
You'll need to make it as a string in feed, something like that:
"blogMetaDesc": "\{'title':'7 tips om goed te slapen tijdens koude winternachten | Dorsoo','description':'Tijdens koude winternachten daalt de slaapkamertemperatuur. Dat kan zorgen voor een slechte nachtrust. Wat kan u doen om goed te slapen? Lees onze 7 tips. '\}"
And create your own target class from example in patch or add some pre-preprocess (e.g. str replace in Tamper) to convert string to JSON before passing to prepareValue().
Comment #20
fbreckx CreditAttribution: fbreckx commentedThanks for your help @artematem! So I modified my JSON feed to this:
Views Data Export makes it hard to export the field blogMetaTitle the right way (the double backslash). But I guess Tamper can do the job.
What isn't clear to me: firstly you need to export it as a string in feed, but then you change it back to JSON?
Could you be a bit more specific?
So with this feed:
Thanks a bunch!
Just for your information: I'm migrating around 200 blogs from D7 to D8. This would come in handy ;)
Comment #21
Frank HH-germany CreditAttribution: Frank HH-germany commentedHi, I'm a little confused.
Can I use the patch like this or do I still have to manually enter the targets in the code?
If the goals still have to be entered, where do they belong?
Then I'll get started and enter all the metategs and schema.org in the code and upload it here.
Greetings from Hamburg-Germany
---
Update
After I uploaded the patch when clicking on assignment the following error was displayed.
The website encountered an unexpected error. Please try again later.
Error: Class 'Drupal\feeds\Feeds\Target\Metatag' not found in Drupal\feeds\Entity\FeedType->getMappingTargets() (line 304 of modules/contrib/feeds/src/Entity/FeedType.php).
and
Fatal error: Cannot declare class Drupal\metatag\Feeds\Target\Metatag, because the name is already in use in .../web/modules/contrib/feeds/src/Feeds/Target/Metatag.php on line 44
Comment #22
Frank HH-germany CreditAttribution: Frank HH-germany commentedWhen troubleshooting, I noticed the following:
in all php files there are more entries from line 5 onwards.
Example Text.php:
Unfortunately I don't really know much about php.
Comment #23
Summit CreditAttribution: Summit commentedHi,
So we need feeds and feeds tamper to get Metatags from Drupal 7 install to Drupal 8 through Feeds?
greetings, Martijn
Comment #24
DamienMcKenna@summit: Any reason to not just use the migrate system for doing the D7->D8 upgrade?
Comment #25
Summit CreditAttribution: Summit commentedHi Damien, Yes! A custom module what I used frequently (+- 800 times), doesn't have migrate integration.
For me as non-programmer is it a too deep learning curve to get this working with migrate, see this try thread; https://www.drupal.org/project/weblinks/issues/3128945
I use a lot specific meta tags within this contenttype. So it would be great to have a Metatag-Feeds solution, so I can import the D7 weblinks to D8 other contenttype with keeping the metatags!
greetings,Martijn
Comment #26
Summit CreditAttribution: Summit commentedHi, I tried patch #13, but I got the following error
What can I do to solve this problem please?
greetings, Martijn
Comment #27
Summit CreditAttribution: Summit commentedHi, Finally working patch #13
I had to change the first line
from +namespace Drupal\metatag\Feeds\Target;
to ++namespace Drupal\feeds\Feeds\Target;
greetings, Martijn
Comment #28
damondt CreditAttribution: damondt commented@Summit You don't want to put a class in another module's namespace.
@kswamy Your patch would address my specific use case (json encoded values), but a better way to go about this would be with multiple properties you can map to. There's an example of this in Drupal\feeds\Feeds\Target\Image
Comment #29
Summit CreditAttribution: Summit commentedHi Damondt,
I understand, but it didn't work without it. Then I got PHP Fatal error: Cannot declare class Drupal\metatag\Feeds\Target\Metatag, because the name is already in use in /modules/contrib/feeds/src/Feeds/Target/Metatag.php on line 43
I am not a good enough programmer to do it correctly may be..
greetings, Martijn
Comment #30
damondt CreditAttribution: damondt commentedI'm attaching a patch that adds a Feeds Target to handle metatag fields. It provides mapping targets for any tags in the "basic" group. The improvements over the last patch include the ability to map to individual tag fields and being source format agnostic. Only the tags in the basic group are provided as targets at the moment as adding targets for all of them is unmanageable due to this issue.
There is a plan to make a generic field target for feeds, but that still won't for metatag field with the way the data is stored as a serialized array.
To Test:
1. Create a feed type mapping to a bundle with a metatag field.
2. Go to the "Mapping" tab of the feed type and select the metatag field as a target
3. Note that all tags in the Basic group are available to map to.
4. Map sources to these targets and note that the import works.
Comment #31
damondt CreditAttribution: damondt commentedComment #32
damondt CreditAttribution: damondt commentedComment #33
ivnish CreditAttribution: ivnish commented#30 works for me
Comment #34
ivnish CreditAttribution: ivnish commentedPlease commit
Comment #35
introfini CreditAttribution: introfini commented#30 works, thanks!
Comment #36
andypostIt still require tests to be ready
Comment #37
DamienMcKennaThis is definitely a step in the right direction, but I don't like how it's currently limited by problems in the Feeds UI, specifically #3173943: Make target selection same height as source selection in mapping form, so I'm not willing to commit it as-is. I would suggest reworking this patch to allow all meta tags, like the D7 version has, and then collaborating on improving the Feeds UI.
Comment #38
someshver CreditAttribution: someshver as a volunteer and commentedI have updated the patch to support OG metatag and Advanced tags.
Comment #39
Summit CreditAttribution: Summit commentedHi,
Can this patch be used to import meta tags through feeds in Drupal 10?
Thanks for your reply in advance!
Greetings, Martijn
Comment #40
DamienMcKennaThat is the intention, yes.
Comment #41
trickfun CreditAttribution: trickfun commentedI apply this patch on
metatag 2.0.0
feed 8.x-3.0-beta4
i get this error
i think feed integration is very important feature to have.
thank you
Comment #42
mNigma07 CreditAttribution: mNigma07 commentedDrupal 10
metatag 2.0.0
#38 Metatag.php replace lines 36-38 with:
$metatag_basic = $metatag_manager->sortedGroupsWithTags()['basic']['tags'] ?? [];
$metatag_og = $metatag_manager->sortedGroupsWithTags()['open_graph']['tags'] ?? [];
$metatag_advanced = $metatag_manager->sortedGroupsWithTags()['advanced']['tags'] ?? [];
Comment #43
djschoone CreditAttribution: djschoone at d-Media commented#42 works. Will look into making a patch
Comment #44
djschoone CreditAttribution: djschoone at d-Media commented#42 works. Made it into a new patch applied to
Comment #45
Jared Nolt CreditAttribution: Jared Nolt commentedPatch #44 worked great in mapping Basic and Advanced schema fields! Would it be possible to make the Product Schema fields available to be mapped and imported also?
Comment #46
DamienMcKennaFor complex setups you might look at Schema.org Blueprints instead of Schema.org Metatag, it's far more flexible in how it works.
I'm moving this to the 2.0.x branch, no further work is being done on the 8.x-1.x branch.
Comment #49
ptmkenny CreditAttribution: ptmkenny commentedAdded a merge request with the code from patch #44 to make it easier to track future work.