Basic question on how to set field programmatically

Created on 10 May 2024, 8 months ago
Updated 13 May 2024, 7 months ago

Problem/Motivation

I know this is a basic question, but I haven't figured it out as of yet and there is very little to nothing about it that I can find on the Interwebs. At the very least, having this info out there will benefit others in the future hopefully. I'm currently on Drupal 9.5.11.

So I am trying to pull in a bunch of data via a feed from our DAMS to create a new image in the Drupal admin. I populate fields programmatically on an add Media screen with the information pulled in. All my data is pulling in great and I have all my fields populated with the needed data *except* the YMD Date field. I've tried a bunch of different ways to populate it, but it remains stubbornly blank. The attached screenshot shows that add media screen I'm using. The "DATE CREATED" field is a ymd_date field. You just fill in the correct record number in that very first field then hit the "Fetch DAMS Record" button and all the data is pulled in and populated to the fields - which is working fine (except this one field).

I've tried populating the individual YYYY MM and DD parts of the ymd_date field separately, and also the whole field together with the YYYYMMDD format. I know the data is there and correct. This is what I've tried (among lots of other things, but I think this is the closest).

Separate components:
$form[$field_key]["widget"][0]["ymd_date"]["year"]["#default_value"] = date('Y',$date_obj); // 4-digit year
$form[$field_key]["widget"][0]["ymd_date"]["month"]["#default_value"] = date('m',$date_obj); // 2-digit month
$form[$field_key]["widget"][0]["ymd_date"]["day"]["#default_value"] = date('d',$date_obj); // 2-digit day

Or, full field:
$form[$field_key]["widget"][0]["#default_value"] = $full_date; // This is in YYYYMMDD format.

Maybe I also need to set the labels of the fields separately? I haven't found any examples anywhere of how to set these fields correctly, and haven't found any documentation except the brief blurb on the project page.

In case it's helpful, the second screenshot shows the "Inspect" of the field.

Can anyone help? Thank you so much!!

πŸ“Œ Task
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pamelalies Minneapolis area

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @pamelalies
  • πŸ‡ΊπŸ‡ΈUnited States pamelalies Minneapolis area
  • πŸ‡ΊπŸ‡ΈUnited States pamelalies Minneapolis area
  • πŸ‡ΊπŸ‡ΈUnited States pamelalies Minneapolis area
  • πŸ‡ΊπŸ‡ΈUnited States pamelalies Minneapolis area

    Hello - just wanted to report that I solved this issue, in case anyone has the same issue and comes across this page. This is the way I finally got it to work (I was able to get past my memory issue and print out the field array on my local):

    $field_key = "field_creation_ymd_date";
    $date_obj = strtotime($date_day);
    // Check if the converted time is good, if it's false then we'll just leave the value alone, if true then update the field.
    if ($date_obj !== false) {
    // Obtained a valid $timestamp; $format is valid, now put it in the fields
    $form[$field_key]["widget"][0]["ymd_date"]["year"]["#value"] = date('Y',$date_obj);
    $form[$field_key]["widget"][0]["ymd_date"]["month"]["#value"] = date('m',$date_obj);
    $form[$field_key]["widget"][0]["ymd_date"]["day"]["#value"] = date('d',$date_obj);
    }

Production build 0.71.5 2024