View source
<?php
namespace Drupal\social_media_links_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class SocialMediaLinksFieldItem extends FieldItemBase {
public static function defaultFieldSettings() {
return [
'platforms' => [],
'iconset' => '',
] + parent::defaultFieldSettings();
}
public static function schema(FieldStorageDefinitionInterface $field) {
return [
'columns' => [
'platform' => [
'type' => 'varchar',
'default' => '',
'length' => '128',
],
'value' => [
'type' => 'varchar',
'default' => '',
'length' => '128',
],
'platform_values' => [
'type' => 'blob',
'not null' => TRUE,
'serialize' => TRUE,
],
],
'indexes' => [
'platform' => [
'platform',
],
'value' => [
'value',
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field) {
$properties['platform'] = DataDefinition::create('string')
->setLabel(t('Platform'));
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Value'));
$properties['platform_values'] = DataDefinition::create('any')
->setLabel(t('Platform Values'));
return $properties;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$platform_settings = $this
->getSetting('platforms');
$element['iconset'] = [
'#type' => 'select',
'#title' => $this
->t('Icon Style'),
'#default_value' => $this
->getSetting('iconset'),
'#options' => \Drupal::service('plugin.manager.social_media_links.iconset')
->getStyles(),
];
$element['platforms'] = [
'#type' => 'table',
'#header' => [
$this
->t('Platform'),
$this
->t('Description'),
$this
->t('Weight'),
],
'#prefix' => '<h5>' . $this
->t('Platforms') . '</h5>',
'#suffix' => '<div class="description">' . $this
->t('The platforms that are allowed to be used in this field. Select none to allow all platforms.') . '</div>',
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'platform-order-weight',
],
],
];
$max_weight = 10;
$platforms = \Drupal::service('plugin.manager.social_media_links.platform')
->getPlatformsSortedByWeight([
'platforms' => $this
->getSetting('platforms'),
]);
foreach ($platforms as $platform_id => $platform) {
$element['platforms'][$platform_id]['#attributes']['class'][] = 'draggable';
$element['platforms'][$platform_id]['#weight'] = $platform['weight'];
if ($platform['weight'] > $max_weight) {
$max_weight = $platform['weight'];
}
$element['platforms'][$platform_id]['enabled'] = [
'#type' => 'checkbox',
'#default_value' => isset($platform_settings[$platform_id]['enabled']) ? $platform_settings[$platform_id]['enabled'] : FALSE,
'#title' => $platform['name']
->render(),
'#title_display' => 'after',
];
$element['platforms'][$platform_id]['description'] = [
'#type' => 'textfield',
'#title' => $platform['name']
->render(),
'#title_display' => 'invisible',
'#description' => $this
->t('The description is used for the title and WAI-ARIA attribute.'),
'#size' => 40,
'#placeholder' => $this
->t('Find / Follow us on %platform', [
'%platform' => $platform['name']
->render(),
]),
'#default_value' => isset($platform_settings[$platform_id]['description']) ? $platform_settings[$platform_id]['description'] : '',
];
$element['platforms'][$platform_id]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for type @type', [
'@type' => $platform_id,
]),
'#title_display' => 'invisible',
'#default_value' => $platform['weight'],
'#attributes' => [
'class' => [
'platform-order-weight',
'platform-order-weight-' . $platform_id,
],
],
'#delta' => $max_weight + count($platforms),
];
}
return $element;
}
public function isEmpty() {
$platform = $this->platform;
$value = $this->value;
$platform_values = $this->platform_values;
if (!empty($platform_values)) {
return FALSE;
}
return $value === NULL || $value === '' || $platform === NULL || $platform === '';
}
}