Commit a7dbbd58 authored by alexpott's avatar alexpott

Issue #2015693 by mordonez, yched: Convert field type to typed data plugin for link module .

parent 67a80147
<?php
/**
* @file
* Contains \Drupal\email\Plugin\field\field_type\LinkItem.
*/
namespace Drupal\link\Plugin\field\field_type;
use Drupal\Core\Entity\Annotation\FieldType;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase;
use Drupal\field\Plugin\Core\Entity\Field;
/**
* Plugin implementation of the 'link' field type.
*
* @FieldType(
* id = "link",
* label = @Translation("Link"),
* description = @Translation("Stores a URL string, optional varchar link text, and optional blob of attributes to assemble a link."),
* instance_settings = {
* "title" = "1"
* },
* default_widget = "link_default",
* default_formatter = "link"
* )
*/
class LinkItem extends ConfigFieldItemBase {
/**
* Definitions of the contained properties.
*
* @var array
*/
static $propertyDefinitions;
/**
* {@inheritdoc}
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['url'] = array(
'type' => 'uri',
'label' => t('URL'),
);
static::$propertyDefinitions['title'] = array(
'type' => 'string',
'label' => t('Link text'),
);
static::$propertyDefinitions['attributes'] = array(
'type' => 'map',
'label' => t('Attributes'),
);
}
return static::$propertyDefinitions;
}
/**
* {@inheritdoc}
*/
public static function schema(Field $field) {
return array(
'columns' => array(
'url' => array(
'description' => 'The URL of the link.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
),
'title' => array(
'description' => 'The link text.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
'attributes' => array(
'description' => 'Serialized array of attributes for the link.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
),
),
);
}
/**
* {@inheritdoc}
*/
public function instanceSettingsForm(array $form, array &$form_state) {
$element = array();
$element['title'] = array(
'#type' => 'radios',
'#title' => t('Allow link text'),
'#default_value' => $this->getFieldDefinition()->getFieldSetting('title'),
'#options' => array(
DRUPAL_DISABLED => t('Disabled'),
DRUPAL_OPTIONAL => t('Optional'),
DRUPAL_REQUIRED => t('Required'),
),
);
return $element;
}
/**
* {@inheritdoc}
*/
public function preSave() {
$item = $this->getValue();
// Trim any spaces around the URL and link text.
$this->url = trim($this->url);
$this->title = trim($this->title);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('url')->getValue();
return $value === NULL || $value === '';
}
}
<?php
/**
* @file
* Contains \Drupal\link\Type\LinkItem.
*/
namespace Drupal\link\Type;
use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem;
/**
* Defines the 'link_field' entity field item.
*/
class LinkItem extends LegacyConfigFieldItem {
/**
* Property definitions of the contained properties.
*
* @see self::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(self::$propertyDefinitions)) {
self::$propertyDefinitions['url'] = array(
'type' => 'uri',
'label' => t('URL'),
);
self::$propertyDefinitions['title'] = array(
'type' => 'string',
'label' => t('Link text'),
);
self::$propertyDefinitions['attributes'] = array(
'type' => 'map',
'label' => t('Attributes'),
);
}
return self::$propertyDefinitions;
}
}
<?php
/**
* @file
* Contains install, update, and uninstall functionality for the Link module.
*/
/**
* Implements hook_field_schema().
*/
function link_field_schema($field) {
$schema['columns']['url'] = array(
'description' => 'The URL of the link.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
);
$schema['columns']['title'] = array(
'description' => 'The link text.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
$schema['columns']['attributes'] = array(
'description' => 'Serialized array of attributes for the link.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
);
return $schema;
}
......@@ -5,8 +5,6 @@
* Defines simple link field types.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
*/
......@@ -20,58 +18,6 @@ function link_help($path, $arg) {
}
}
/**
* Implements hook_field_info().
*/
function link_field_info() {
$types['link'] = array(
'label' => t('Link'),
'description' => t('Stores a URL string, optional varchar link text, and optional blob of attributes to assemble a link.'),
'instance_settings' => array(
'title' => DRUPAL_OPTIONAL,
),
'default_widget' => 'link_default',
'default_formatter' => 'link',
'class' => '\Drupal\link\Type\LinkItem',
);
return $types;
}
/**
* Implements hook_field_instance_settings_form().
*/
function link_field_instance_settings_form($field, $instance) {
$form['title'] = array(
'#type' => 'radios',
'#title' => t('Allow link text'),
'#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : DRUPAL_OPTIONAL,
'#options' => array(
DRUPAL_DISABLED => t('Disabled'),
DRUPAL_OPTIONAL => t('Optional'),
DRUPAL_REQUIRED => t('Required'),
),
);
return $form;
}
/**
* Implements hook_field_is_empty().
*/
function link_field_is_empty($item, $field_type) {
return !isset($item['url']) || $item['url'] === '';
}
/**
* Implements hook_field_presave().
*/
function link_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => &$item) {
// Trim any spaces around the URL and link text.
$item['url'] = trim($item['url']);
$item['title'] = trim($item['title']);
}
}
/**
* Implements hook_theme().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment