SocialLinkItem.php in Social Link Field 8
File
src/Plugin/Field/FieldType/SocialLinkItem.php
View source
<?php
namespace Drupal\social_link_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
class SocialLinkItem extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['social'] = DataDefinition::create('string')
->setLabel(t('Social network'))
->setRequired(FALSE);
$properties['link'] = DataDefinition::create('string')
->setLabel(t('Profile link'))
->setRequired(FALSE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema['columns'] = [
'social' => [
'type' => 'varchar',
'length' => 255,
],
'link' => [
'type' => 'varchar',
'length' => 255,
],
];
return $schema;
}
public function isEmpty() {
$value = $this
->get('link')
->getValue();
$social = $this
->get('social')
->getValue();
return empty($value) && empty($social);
}
public function preSave() {
$link = $this
->get('link')
->getValue();
$social = $this
->get('social')
->getValue();
if (empty($link) && !empty($social)) {
$this
->set('social', '');
}
if (empty($social) && !empty($link)) {
$this
->set('social', '');
}
parent::preSave();
}
public static function mainPropertyName() {
return 'link';
}
public static function defaultFieldSettings() {
return [
'platforms' => [],
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form = parent::fieldSettingsForm($form, $form_state);
$platforms_service = \Drupal::service('plugin.manager.social_link_field.platform');
$options = [];
foreach ($platforms_service
->getPlatforms() as $platform) {
$options[$platform['id']] = $platform['name']
->getUntranslatedString();
}
$platforms = $this
->getSetting('platforms') ?? [];
$form['platforms'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed Platforms'),
'#options' => $options,
'#default_value' => $platforms,
'#description' => $this
->t('Select none to allow all'),
];
return $form;
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$constraints[] = $constraint_manager
->create('ComplexData', [
'link' => [
'Regex' => [
'message' => $this
->t('Just add the part of your profile. Do not include preceding slash.'),
'pattern' => '/^(?!(http:\\/\\/|https:\\/\\/|www.|\\/))/i',
],
],
]);
return $constraints;
}
}