View source
<?php
namespace Drupal\paragraphs_grid\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
class GridFieldType extends FieldItemBase {
protected $moduleConfig;
protected $gridConfig;
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
$config_factory = \Drupal::service('config.factory');
$this->moduleConfig = $config_factory
->get('paragraphs_grid.settings');
$this->gridConfig = $config_factory
->get($this->moduleConfig
->get('gridtype'));
}
public static function defaultStorageSettings() {
return [
'optional' => [
'offset',
],
'view_modes_enabled' => TRUE,
] + parent::defaultStorageSettings();
}
protected function cellPropsOptions() {
$options = [];
foreach ($this->gridConfig
->get('cell-properties') as $def) {
if ($def['optional']) {
$options[$def['name']] = $def['label'];
}
}
return $options;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$elements = [];
$settings = $this
->getSetting('optional');
$elements['optional'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Grid properties'),
'#description' => $this
->t('WARNING: Limit the number of props to a minimum to avoid overloaded form elements.'),
'#default_value' => $settings,
'#options' => $this
->cellPropsOptions(),
];
$elements['view_modes_enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('View modes enabled'),
'#description' => $this
->t('Enable view mode selector in paragraph edit form. (View mode selector is only visible if there are view modes to select.)'),
'#default_value' => $this
->getSetting('view_modes_enabled'),
];
return $elements;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Grid classes'))
->setSetting('case_sensitive', TRUE)
->setRequired(FALSE);
$properties['view_mode'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('View mode'))
->setSetting('case_sensitive', TRUE)
->setRequired(FALSE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 512,
],
'view_mode' => [
'type' => 'varchar',
'length' => 64,
],
],
];
return $schema;
}
public function getConstraints() {
$constraints = parent::getConstraints();
return $constraints;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$values['value'] = $random
->word(mt_rand(1, 511));
$values['view_mode'] = $random
->word(mt_rand(1, 63));
return $values;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
$view_mode = $this
->get('view_mode')
->getValue();
return ($value === NULL || $value === '') && ($view_mode === NULL || $view_mode === '');
}
}