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.
Hi, I have a content type with a multiple youtube field type. Now, when I try to programmatically save this field, no matter what I do, It remains blank.
I do it this way:
$node->field_youtube_video[$node->language][]['value']="http://www.youtube.com/watch?v=jRPDvD4kiI8";
node_save( $node );
It creates all the fields, but the value is empty.
Thanks in advance! Diego
Comments
Comment #1
Pasqualletry:
Comment #2
John Pitcairn CreditAttribution: John Pitcairn commentedYou probably shouldn't assume a particular language, so that should really be:
Comment #3
PasqualleBut afaik field language is not the same as node language. Those are 2 different things.
So for a non translatable field the language should be LANGUAGE_NONE
Comment #4
guschilds CreditAttribution: guschilds commentedIf you use Devel to examine a YouTube field created via GUI, you'll notice there is 'input' alongside 'video_id'.
'input' is the full URL given to the field. The 'video_id' is then derived from that by the module.
To properly save a YouTube field programmatically, you should really also provide the full URL for 'input':
'input' is important to have because:
If you were batch saving nodes and only had the 'video_id', you could construct an appropriate 'input' by simply adding 'http://www.youtube.com/watch?v=' before the 'video_id'.
We will add this information to the README when it is created for #1616690: Create a README.txt explaining valid YouTube URLS.
As for the language debate, I believe @Pasqualle is correct in saying that node and field languages are not the same thing and this field is not translatable, therefore I went with his use of LANGUAGE_NONE above.
Comment #6
gzveri CreditAttribution: gzveri commentedIn Drupal8 here's the working code: