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 0000000000000000000000000000000000000000..0926a1e54ac03fe7669092f7e90ce0fee861d4ac --- /dev/null +++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php @@ -0,0 +1,104 @@ + '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 0000000000000000000000000000000000000000..a0b0e96c00493a8e262594d69d531e8154bc10c3 --- /dev/null +++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Plugin/field/widget/TelephoneDefaultWidget.php @@ -0,0 +1,39 @@ + '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 0000000000000000000000000000000000000000..5ef4e4caf116ab5fedfa44e405c198cf484485e3 --- /dev/null +++ b/core/modules/field/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php @@ -0,0 +1,95 @@ + '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 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('', '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 0000000000000000000000000000000000000000..9ac6dacacc9a4f42990475631610fe77ba7d823f --- /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 0000000000000000000000000000000000000000..d409b8b12b5119cac42011a37d789b6a206375bc --- /dev/null +++ b/core/modules/field/modules/telephone/telephone.install @@ -0,0 +1,22 @@ + 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 0000000000000000000000000000000000000000..493f7d1147c7f0280a64cd013737e7316128c90f --- /dev/null +++ b/core/modules/field/modules/telephone/telephone.module @@ -0,0 +1,45 @@ + 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'; + } +}