summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDries2013-01-18 18:17:06 (GMT)
committerDries2013-01-18 18:17:06 (GMT)
commitef658a6b6276887b9f4c3713bd6c0d1c22a1d477 (patch)
treef751beebfac1e43b35748c0d83e20bfe041ee18b
parent1415a436ffbee7b674a1a43b3d20f8905ea71872 (diff)
Issue #1740470 by nick_schuch, rbayliss, patrickd, lliss, larowlan, webflo: added a new phone number field type.
-rw-r--r--core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php104
-rw-r--r--core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/widget/TelephoneDefaultWidget.php39
-rw-r--r--core/modules/field/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php95
-rw-r--r--core/modules/field/modules/telephone/telephone.info6
-rw-r--r--core/modules/field/modules/telephone/telephone.install22
-rw-r--r--core/modules/field/modules/telephone/telephone.module45
6 files changed, 311 insertions, 0 deletions
diff --git a/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php
new file mode 100644
index 0000000..0926a1e
--- /dev/null
+++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php
@@ -0,0 +1,104 @@
+<?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;
+ }
+}
diff --git a/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/widget/TelephoneDefaultWidget.php b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/widget/TelephoneDefaultWidget.php
new file mode 100644
index 0000000..a0b0e96
--- /dev/null
+++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/widget/TelephoneDefaultWidget.php
@@ -0,0 +1,39 @@
+<?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;
+ }
+
+}
diff --git a/core/modules/field/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php b/core/modules/field/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php
new file mode 100644
index 0000000..5ef4e4c
--- /dev/null
+++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php
@@ -0,0 +1,95 @@
+<?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.');
+ }
+}
diff --git a/core/modules/field/modules/telephone/telephone.info b/core/modules/field/modules/telephone/telephone.info
new file mode 100644
index 0000000..9ac6dac
--- /dev/null
+++ b/core/modules/field/modules/telephone/telephone.info
@@ -0,0 +1,6 @@
+name = Telephone
+description = Defines a field type for telephone numbers.
+package = Core
+version = VERSION
+core = 8.x
+dependencies[] = field
diff --git a/core/modules/field/modules/telephone/telephone.install b/core/modules/field/modules/telephone/telephone.install
new file mode 100644
index 0000000..d409b8b
--- /dev/null
+++ b/core/modules/field/modules/telephone/telephone.install
@@ -0,0 +1,22 @@
+<?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,
+ );
+}
diff --git a/core/modules/field/modules/telephone/telephone.module b/core/modules/field/modules/telephone/telephone.module
new file mode 100644
index 0000000..493f7d1
--- /dev/null
+++ b/core/modules/field/modules/telephone/telephone.module
@@ -0,0 +1,45 @@
+<?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';
+ }
+}