0

I have a content type that has an image field that allows you to upload up to 4 photos. I'm creating a form for users to use that will allow them to edit existing items of this content type. The form fields need to be filled with the existing node field values by default. Retrieving fields such as textfields and supplying the default value works just fine, like so:

class EquipmentEditForm extends ConfigFormBase {
    private $imageNum = 4;

    protected function getEditableConfigNames() {
        return array(
            'equipment.form',
        );
    }
    public function buildForm(array $form, FormStateInterface $form_state) {
        $equipment_id = 10306;
        $node = NODE::load($equipment_id);

        $contact        = $node->get('field_contact')->value;
        $description    = $node->get('field_description')->value;

        $form = [
            'container'    => [
                '#type'       => 'container',
                '#attributes' => [
                    'class'      => ['equipment-entry'],
                ],
                'contact'      => [
                    '#type'       => 'textfield',
                    '#title'      => t('Contact Person'),
                    '#attributes' => [
                        'name'       => 'contact',
                    ],
                    '#default_value' => $contact,
                ],
                'description'    => [
                    '#type'       => 'textfield',
                    '#title'      => t('Description'),
                    '#attributes' => [
                        'name'       => 'description',
                    ],
                    '#default_value' => $description,
                ],
           ],
        ];
    }
}

This generates the form and populated the fields from the node just fine. However I'm not sure how to populate the image field. I've attempted the following with no luck.

    if ($node->get('field_equipment_photo')->entity != null) {
        $photos = $node->get('field_equipment_photo');
    }
    for ($i = 1; $i <= $this->imageNum; $i += 1) {

        $title = ($i == 1)?'Image '.$i:'Image '.$i.' (extra)';

        $form['image'.$i] = [
            '#type'            => 'managed_file',
            '#upload_location' => 'public://images/',
            '#title'           => t($title),
            '#name'            => 'image'.$i,
            '#default_value'   => $photos[$i],
        ];
    }

It just provides an error

Recoverable fatal error: Object of class Drupal\Core\TypedData\Plugin\DataType\IntegerData could not be converted to string in Drupal\Core\Entity\ContentEntityStorageBase->buildCacheId() (line 659 of core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).

I know I'm retrieving the images because if I perform a statement like this:

die(print_r($photos[1]->entity));

I do get a list of the information contained in the 2 uploaded image. Including the filename.

How would I programmatically retrieve the values of an image field array of a node and place them into the default values of a form field?

UPDATE: I'm able to discover the fid using

die(print_r($photos[0]->entity->fid->value));

but using $photos[0]->entity->fid->value as the default value leads to this error

Warning: Invalid argument supplied for foreach() in Drupal\file\Element\ManagedFile::valueCallback()

0

Turns out I needed to get the fid of the images and place them into an array. First I started by retrieving the images like I did before

    if ($node->get('field_equipment_photo')->entity != null) {
        $photos = $node->get('field_equipment_photo');
    }

Then I grabbed the fid and placed them into the default value like so

if ($photos[$i]->entity->fid->value != null) {
                $form['image'.$i]['#default_value'] = array($photos[$i]->entity->fid->value);
            }

For some reason even though the fid is just one value it still needs to be wrapped in an array when being used as the default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.