Commit ebfdd023 authored by targoo's avatar targoo

WIP - enable default formater and widget

parent 801c0986
......@@ -24,7 +24,7 @@ function color_field_help($path, $arg) {
*/
function color_field_field_info() {
$types['color_field'] = array(
'label' => t('Link'),
'label' => t('Color Field'),
'description' => t('Field using a hexadecimal notation.'),
'instance_settings' => array(
'title' => DRUPAL_OPTIONAL,
......@@ -52,9 +52,9 @@ function color_field_field_instance_settings_form($field, $instance) {
'#title' => t('Allow link title'),
'#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'),
DRUPAL_DISABLED => t('Disabled'),
DRUPAL_OPTIONAL => t('Optional'),
DRUPAL_REQUIRED => t('Required'),
),
);
return $form;
......
<?php
/**
* @file
* Contains \Drupal\telephone\Plugin\field\formatter\TelephoneLinkFormatter.
*/
namespace Drupal\telephone\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
/**
* Plugin implementation of the 'telephone_link' formatter.
*
* @Plugin(
* id = "telephone_link",
* module = "telephone",
* label = @Translation("Telephone link"),
* field_types = {
* "telephone"
* },
* settings = {
* "title" = ""
* }
* )
*/
class TelephoneLinkFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$elements['title'] = array(
'#type' => 'textfield',
'#title' => t('Title to replace basic numeric telephone number display.'),
'#default_value' => $this->getSetting('title'),
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$settings = $this->getSettings();
if (!empty($settings['title'])) {
$summary[] = t('Link using text: @title', array('@title' => $settings['title']));
}
else {
$summary[] = t('Link using provided telephone number.');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function prepareView(array $entities, $langcode, array &$items) {
$settings = $this->getSettings();
foreach ($entities as $id => $entity) {
foreach ($items[$id] as &$item) {
// If available, set custom link text.
if (!empty($settings['title'])) {
$item['title'] = $settings['title'];
}
// Otherwise, use telephone number itself as title.
else {
$item['title'] = $item['value'];
}
}
}
}
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$element = array();
foreach ($items as $delta => $item) {
// Prepend 'tel:' to the telephone number.
$href = 'tel:' . rawurlencode(preg_replace('/\s+/', '', $item['value']));
// Render each element as link.
$element[$delta] = array(
'#type' => 'link',
'#title' => $item['title'],
'#href' => $href,
'#options' => array('external' => TRUE),
);
}
return $element;
}
}
<?php
/**
* @file
* Contains \Drupal\color_field\Plugin\field\formatter\ColorFieldHexFormatter.
*/
namespace Drupal\color_field\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
/**
* Plugin implementation of the 'color_field_hex' formatter.
*
* @Plugin(
* id = "color_field_default",
* module = "color_field",
* label = @Translation("Color HEX"),
* field_types = {
* "color_field"
* }
* )
*/
class ColorFieldHexFormatter extends FormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#type' => 'link',
'#title' => $item['value'],
'#href' => 'mailto:' . $item['value'],
);
}
return $elements;
}
}
......@@ -2,53 +2,56 @@
/**
* @file
* Contains \Drupal\telephone\Plugin\field\widget\TelephoneDefaultWidget.
* Contains \Drupal\color_field\Plugin\field\widget\ColorFieldDefaultWidget.
*/
namespace Drupal\telephone\Plugin\field\widget;
namespace Drupal\color_field\Plugin\field\widget;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
/**
* Plugin implementation of the 'telephone_default' widget.
* Plugin implementation of the 'color_field_default' widget.
*
* @Plugin(
* id = "telephone_default",
* module = "telephone",
* label = @Translation("Telephone number"),
* id = "color_field_default",
* module = "color_field",
* label = @Translation("Color field"),
* field_types = {
* "telephone"
* "color_field"
* },
* settings = {
* "placeholder" = ""
* "placeholder_title" = ""
* }
* )
*/
class TelephoneDefaultWidget extends WidgetBase {
class ColorFieldDefaultWidget extends WidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
* Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
*/
public function settingsForm(array $form, array &$form_state) {
$element['placeholder'] = array(
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$element['value'] = $element + array(
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this->getSetting('placeholder'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
'#maxlength' => 6,
'#title' => t('Color Field'),
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#placeholder' => $this->getSetting('placeholder'),
'#required' => $element['#required'],
);
return $element;
}
/**
* Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
*/
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$element['value'] = $element + array(
'#type' => 'tel',
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#placeholder' => $this->getSetting('placeholder'),
public function settingsForm(array $form, array &$form_state) {
$element['placeholder'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this->getSetting('placeholder'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
return $element;
}
......
......@@ -10,14 +10,14 @@ namespace Drupal\color_field\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'email_field' entity field item.
* Defines the 'color_field' entity field item.
*/
class ColorFieldItem extends FieldItemBase {
/**
* Definitions of the contained properties.
*
* @see EmailItem::getPropertyDefinitions()
* @see ColorFieldItem::getPropertyDefinitions()
*
* @var array
*/
......
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