Skip to content

Instantly share code, notes, and snippets.

@leymannx
Created October 18, 2019 07:33
Show Gist options
  • Save leymannx/da0727b3e3f1d013e6468cbd4b5c5c01 to your computer and use it in GitHub Desktop.
Save leymannx/da0727b3e3f1d013e6468cbd4b5c5c01 to your computer and use it in GitHub Desktop.
Drupal 8 file upload block
<?php
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'Site Logo' Block.
*
* @Block(
* id = "mymodule_site_logo_block",
* admin_label = @Translation("Site Logo"),
* category = @Translation("Custom"),
* )
*/
class SiteLogoBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The file usage interface.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
/**
* SiteLogoBlock constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
* File usage interface.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FileUsageInterface $file_usage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileUsage = $file_usage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('file.usage')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'label_display' => FALSE,
'primary_logo' => '',
'secondary_logo' => '',
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['logos'] = [
'#type' => 'fieldset',
'#title' => $this->t('Logos'),
];
$primary_logo = $this->configuration['primary_logo'];
$form['logos']['primary_logo'] = [
'#type' => 'managed_file',
'#title' => $this->t('Primary Logo'),
'#default_value' => $primary_logo,
'#upload_location' => 'public://site-logo/',
'#upload_validators' => [
'file_validate_extensions' => ['png'],
],
'#description' => $this->t('Allowed extensions: png'),
];
$form['logos']['primary_logo_fid_exists'] = [
'#type' => 'value',
'#value' => isset($primary_logo[0]) ? $primary_logo[0] : FALSE,
];
$secondary_logo = $this->configuration['secondary_logo'];
$form['logos']['secondary_logo'] = [
'#type' => 'managed_file',
'#title' => $this->t('Secondary Logo'),
'#default_value' => $secondary_logo,
'#upload_location' => 'public://site-logo/',
'#upload_validators' => [
'file_validate_extensions' => ['png'],
],
'#description' => $this->t('Allowed extensions: png'),
];
$form['logos']['secondary_logo_fid_exists'] = [
'#type' => 'value',
'#value' => isset($secondary_logo[0]) ? $secondary_logo[0] : FALSE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$logos = [
'primary_logo',
'secondary_logo',
];
foreach ($logos as $logo) {
// 1. Get form value and save it.
$image = $form_state->getValue(['logos', $logo]);
$this->configuration[$logo] = $image;
// 2. Handle file upload.
/** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
$file_usage = $this->fileUsage;
if ($file_id = isset($image[0]) ? $image[0] : FALSE) {
/** @var \Drupal\file\Entity\File $file */
$file = File::load($file_id);
$file->setPermanent();
$file->save();
$file_usage->add($file, 'ix_header', 'ix_header', $file->id());
}
if ($existing_fid = $form_state->getValue(['logos', "{$logo}_fid_exists",])) {
/** @var \Drupal\file\Entity\File $existing_file */
$existing_file = File::load($existing_fid);
if ($existing_fid !== $file_id) {
$file_usage->delete($existing_file, 'ix_header', 'ix_header', $existing_file->id(), 0);
}
}
}
}
/**
* {@inheritdoc}
*/
public function build() {
// mymodule/templates/mymodule-site-logo.html.twig.
return [
'#theme' => 'mymodule_site_logo',
// @todo Pass images to template and render.
//'#primary_logo' => $this->configuration['primary_logo'],
//'#secondary_logo' => $this->configuration['secondary_logo'],
'#primary_logo' => [],
'#secondary_logo' => [],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment