Block configuration form: problem adding a file field | Drupal 8

by UnqtiousDude   Last Updated December 27, 2017 18:07 PM

The text field I added to my custom block module works great. However, I added a file field fails at some point. I submit the block with the fields entered, but when you reenter the configuration, only the text field appears to saved. The file field wants you to upload the file again.

When I CD into the file folder (cd /web/sites/default/files/block/image.png), the image appears to have been uploaded successfully. I am not sure why it turns up missing.

Xdebug and the error log hasn't resulted in anything useful. This other post on the subject didn't help either: How to add an image(field) to a block?

Any help would be appreciated.

/**
 * {@inheritdoc}
 */
public function blockForm($form, FormStateInterface $form_state)
{
    $form = parent::blockForm($form, $form_state);

    $config = $this->getConfiguration();

    $address = $this->configuration['address'];
    $brand_logo = $this->configuration['brand_logo'];

    $validators = array(
        'file_validate_is_image' => array(),
        'file_validate_extensions' => array('gif png jpg jpeg'),
        'file_validate_size' => array(25600000)
    );

    $form['brand_logo'] = [
        '#type' => 'managed_file',
        '#name' => 'brand_logo',
        '#title' => t('Brand Logo'),
        '#size' => 20,
        '#multiple' => FALSE,
        '#description' => t('Allowed images'),
        '#upload_validators' => $validators,
        '#upload_location' => 'public://brand_logo/',
        '#default_value' => $brand_logo['value'],
    ];

    $form['address'] = [
        '#type' => 'text_format',
        '#title' => $this->t('address'),
        '#description' => $this->t('Add custom address.'),
        '#default_value' => $address['value'],
        '#format' => $address['format'],
    ];

    return $form;
}

/**
 * {@inheritdoc}
 */
public function blockSubmit($form, FormStateInterface $form_state)
{
    parent::blockSubmit($form, $form_state);
    $brand_logo = $form_state->getValue('brand_logo');
    $this->configuration['brand_logo'] = $brand_logo;
    $file = File::load($brand_logo[0]);
    $file->setPermanent();
    $file->save();
    $this->configuration['address'] = $form_state->getValue('address');
}

/**
 * {@inheritdoc}
 */
public function build()
{
    $build = [];

    if (isset($this->configuration['brand_logo']) && !empty($this->configuration['brand_logo'])) {

        $image_field = $this->configuration['brand_logo'];
        $image_uri = File::load($image_field[0]);

        $build['brand_logo'] = [
            '#theme' => 'rtd_footer',
            '#style_name' => 'ourcustomername',
            '#uri' => $image_uri->uri->value
        ];

    } else {
        $build['brand_logo']['#markup'] = '[' . t('Picture') . ']';
    }


    return $build;
}
Tags : 8 forms fields blocks


Related Questions


Add link field to block form in Drupal 8

Updated June 08, 2017 12:07 PM



How to get a field value of custom block?

Updated April 06, 2017 06:07 AM

How to connect type and block programmatically

Updated July 08, 2017 20:07 PM