View source
<?php
namespace Drupal\minisite\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\file\Plugin\Field\FieldType\FileItem;
use Drupal\minisite\Minisite;
use Drupal\minisite\MinisiteInterface;
class MinisiteItem extends FileItem {
use StringTranslationTrait;
public static function defaultFieldSettings() {
$defaults = [
'file_extensions' => MinisiteInterface::SUPPORTED_ARCHIVE_EXTENSIONS,
'file_directory' => MinisiteInterface::ARCHIVE_UPLOAD_DIR,
'minisite_extensions' => MinisiteInterface::ALLOWED_EXTENSIONS,
'description_field' => TRUE,
];
$settings = $defaults + parent::defaultFieldSettings();
return $settings;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'target_id' => [
'description' => 'The ID of the file entity.',
'type' => 'int',
'unsigned' => TRUE,
],
'description' => [
'description' => 'A description of the file.',
'type' => 'text',
],
'asset_path' => [
'description' => 'The URI of the entry point minisite asset path (index.html).',
'type' => 'varchar',
'length' => 255,
],
'options' => [
'description' => 'Serialized array of options for the link.',
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
],
'alias_status' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
],
],
'indexes' => [
'target_id' => [
'target_id',
],
'asset_path' => [
'asset_path',
],
],
'foreign keys' => [
'target_id' => [
'table' => 'file_managed',
'columns' => [
'target_id' => 'fid',
],
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
unset($properties['display']);
$properties['asset_path'] = DataDefinition::create('string')
->setLabel(t('Minisite asset path'));
$properties['options'] = MapDataDefinition::create()
->setLabel(t('Options'));
$properties['alias_status'] = DataDefinition::create('boolean')
->setLabel(t('Minisite URL alias status'));
return $properties;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$settings = $this
->getFieldDefinition()
->getFieldStorageDefinition()
->getSettings();
$scheme_options = \Drupal::service('stream_wrapper_manager')
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
$element['uri_scheme'] = [
'#type' => 'radios',
'#title' => $this
->t('Upload destination'),
'#options' => $scheme_options,
'#default_value' => $settings['uri_scheme'],
'#description' => $this
->t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
];
return $element;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this
->getSettings();
$element['file_extensions']['#title'] = $this
->t('Allowed archive file extensions');
if (!\Drupal::currentUser()
->hasPermission('administer site configuration')) {
$element['file_extensions']['#disabled'] = TRUE;
}
$extensions = str_replace(' ', ', ', $settings['minisite_extensions']);
$element['minisite_extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Allowed file extensions in uploaded minisite files'),
'#default_value' => $extensions,
'#description' => $this
->t('Separate extensions with a space or comma and do not include the leading dot.'),
'#element_validate' => [
[
get_class($this),
'validateExtensions',
],
[
get_class($this),
'validateNoDeniedExtensions',
],
],
'#weight' => 11,
'#maxlength' => 256,
'#required' => TRUE,
];
return $element;
}
public static function validateNoDeniedExtensions($element, FormStateInterface $form_state) {
if (!empty($element['#value'])) {
$extensions = preg_replace('/([, ]+\\.?)/', ' ', trim(strtolower($element['#value'])));
$extensions = array_filter(explode(' ', $extensions));
$denied_extensions = explode(' ', MinisiteInterface::DENIED_EXTENSIONS);
$invalid_extensions = array_intersect($extensions, $denied_extensions);
if (count($invalid_extensions) > 0) {
$form_state
->setError($element, t('The list of allowed extensions is not valid, be sure to not include %ext extension(s).', [
'%ext' => implode(', ', $invalid_extensions),
]));
}
}
}
public function postSave($update) {
$item_list = $this
->getParent();
if (!$item_list
->isEmpty()) {
$this
->createMinisite($item_list);
}
return TRUE;
}
public function delete() {
$item_list = $this
->getParent();
if (!$item_list
->isEmpty()) {
$this
->deleteMinisite($item_list);
}
}
public function isDisplayed() {
return TRUE;
}
protected function createMinisite(FieldItemList $item_list) {
$minisite = Minisite::createInstance($item_list);
if ($minisite) {
$minisite
->save();
$this->asset_path = $minisite
->getIndexAssetUri();
}
}
protected function deleteMinisite(FieldItemList $item_list) {
$minisite = Minisite::createInstance($item_list);
if ($minisite) {
$minisite
->delete();
}
}
}