View source
<?php
namespace Drupal\simple_iframe\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;
class SimpleIframeFieldType extends FieldItemBase {
public static function defaultFieldSettings() {
return [
'width' => '100%',
'height' => '',
] + parent::defaultFieldSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['url'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('URL'));
$properties['width'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Width'));
$properties['height'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Height'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'url' => [
'description' => 'The URL of the iframe.',
'type' => 'text',
'length' => 2048,
'not null' => FALSE,
],
'width' => [
'description' => 'The iframe width.',
'type' => 'text',
'length' => 255,
'not null' => FALSE,
],
'height' => [
'description' => 'The iframe height.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
],
];
return $schema;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$tlds = [
'com',
'net',
'org',
];
$domain_length = mt_rand(7, 15);
$random = new Random();
$values['url'] = '//' . $random
->word($domain_length) . '.' . $tlds[mt_rand(0, count($tlds) - 1)];
return $values;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$elements['width'] = [
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $this
->getSetting('width'),
'#required' => TRUE,
'#description' => t('Default width of iframe. Set a number or %'),
];
$elements['height'] = [
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $this
->getSetting('height'),
'#required' => TRUE,
'#description' => t('Default height of iframe.'),
];
return $elements;
}
public function isEmpty() {
$value = $this
->get('url')
->getValue();
return $value === NULL || $value === '';
}
}