Commit ef658a6b authored by Dries's avatar Dries

Issue #1740470 by nick_schuch, rbayliss, patrickd, lliss, larowlan, webflo:...

Issue #1740470 by nick_schuch, rbayliss, patrickd, lliss, larowlan, webflo: added a new phone number field type.
parent 1415a436
<?php
/**
* @file
* Definition of Drupal\telephone\Plugin\field\formatter\TelephoneLinkFormatter.
*/
namespace Drupal\telephone\Plugin\field\formatter;
use Drupal\Core\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 {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
*/
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;
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary().
*/
public function settingsSummary() {
$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;
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().
*/
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'];
}
}
}
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
*/
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
* Definition of Drupal\telephone\Plugin\field\widget\TelephoneDefaultWidget.
*/
namespace Drupal\telephone\Plugin\field\widget;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
/**
* Plugin implementation of the 'telephone_default' widget.
*
* @Plugin(
* id = "telephone_default",
* module = "telephone",
* label = @Translation("Telephone number"),
* field_types = {
* "telephone"
* }
* )
*/
class TelephoneDefaultWidget extends WidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
*/
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,
);
return $element;
}
}
<?php
/**
* @file
* Definition of Drupal\telephone\TelephoneFieldTest.
*/
namespace Drupal\telephone\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests the creation of telephone fields.
*/
class TelephoneFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array(
'field',
'field_sql_storage',
'node',
'telephone'
);
protected $instance;
protected $web_user;
public static function getInfo() {
return array(
'name' => 'Telephone field',
'description' => "Test the creation of telephone fields.",
'group' => 'Field types'
);
}
function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'article'));
$this->article_creator = $this->drupalCreateUser(array('create article content', 'edit own article content'));
$this->drupalLogin($this->article_creator);
}
// Test fields.
/**
* Helper function for testTelephoneField().
*/
function testTelephoneField() {
// Add the telepone field to the article content type.
$field = array(
'field_name' => 'field_telephone',
'type' => 'telephone',
);
field_create_field($field);
$instance = array(
'field_name' => 'field_telephone',
'label' => 'Telephone Number',
'entity_type' => 'node',
'bundle' => 'article',
);
field_create_instance($instance);
entity_get_display('node', 'article', 'default')
->setComponent('field_telephone', array(
'type' => 'telephone_link',
'weight' => 1,
))
->save();
// Test basic entery of telephone field.
$edit = array(
"title" => $this->randomName(),
"field_telephone[und][0][value]" => "123456789",
);
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertRaw('<a href="tel:123456789">', 'A telephone link is provided on the article node page.');
// Add number with a space in it. Need to ensure it is stripped.
$edit = array(
"title" => $this->randomName(),
"field_telephone[und][0][value]" => "1234 56789",
);
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertRaw('<a href="tel:123456789">', 'Telephone link is output with whitespace removed.');
}
}
name = Telephone
description = Defines a field type for telephone numbers.
package = Core
version = VERSION
core = 8.x
dependencies[] = field
<?php
/**
* @file
* Install, update and uninstall functions for the Telephone module.
*/
/**
* Implements hook_field_schema().
*/
function telephone_field_schema($field) {
$columns = array(
'value' => array(
'type' => 'varchar',
'length' => 256,
'not null' => FALSE,
),
);
return array(
'columns' => $columns,
);
}
<?php
/**
* @file
* Defines a simple telephone number field type.
*/
/**
* Implements hook_field_info().
*/
function telephone_field_info() {
return array(
'telephone' => array(
'label' => t('Telephone number'),
'description' => t('This field stores a telephone number in the database.'),
'default_widget' => 'telephone_default',
'default_formatter' => 'telephone_link',
),
);
}
/**
* Implements hook_field_info_alter().
*/
function telephone_field_info_alter(&$info) {
if (module_exists('text')) {
$info['telephone']['default_formatter'] = 'text_plain';
}
}
/**
* Implements hook_field_is_empty().
*/
function telephone_field_is_empty($item, $field) {
return !isset($item['value']) || $item['value'] === '';
}
/**
* Implements hook_field_formatter_info_alter().
*/
function telephone_field_formatter_info_alter(&$info) {
if (isset($info['text_plain'])) {
$info['text_plain']['field_types'][] = 'telephone';
}
}
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