Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,17 @@
# Schema for the configuration files of the Telephone module.
field.formatter.settings.telephone_link:
type: mapping
label: 'Telephone link format settings'
mapping:
title:
type: label
label: 'Title to replace basic numeric telephone number display'
field.widget.settings.telephone_default:
type: mapping
label: 'Telephone default format settings'
mapping:
placeholder:
type: label
label: 'Placeholder'

View file

@ -0,0 +1,97 @@
<?php
/**
* @file
* Contains \Drupal\telephone\Plugin\Field\FieldFormatter\TelephoneLinkFormatter.
*/
namespace Drupal\telephone\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'telephone_link' formatter.
*
* @FieldFormatter(
* id = "telephone_link",
* label = @Translation("Telephone link"),
* field_types = {
* "telephone"
* }
* )
*/
class TelephoneLinkFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'title' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $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 viewElements(FieldItemListInterface $items) {
$element = array();
$title_setting = $this->getSetting('title');
foreach ($items as $delta => $item) {
// Render each element as link.
$element[$delta] = array(
'#type' => 'link',
// Use custom title if available, otherwise use the telephone number
// itself as title.
'#title' => $title_setting ?: $item->value,
// Prepend 'tel:' to the telephone number.
'#url' => Url::fromUri('tel:' . rawurlencode(preg_replace('/\s+/', '', $item->value))),
'#options' => array('external' => TRUE),
);
if (!empty($item->_attributes)) {
$element[$delta]['#options'] += array('attributes' => array());
$element[$delta]['#options']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
return $element;
}
}

View file

@ -0,0 +1,90 @@
<?php
/**
* @file
* Contains \Drupal\telephone\Plugin\Field\FieldType\TelephoneItem.
*/
namespace Drupal\telephone\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'telephone' field type.
*
* @FieldType(
* id = "telephone",
* label = @Translation("Telephone number"),
* description = @Translation("This field stores a telephone number in the database."),
* category = @Translation("Number"),
* default_widget = "telephone_default",
* default_formatter = "basic_string"
* )
*/
class TelephoneItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'varchar',
'length' => 256,
),
),
);
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Telephone number'))
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints = parent::getConstraints();
$max_length = 256;
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: the telephone number may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)),
)
),
));
return $constraints;
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['value'] = rand(pow(10, 8), pow(10, 9)-1);
return $values;
}
}

View file

@ -0,0 +1,78 @@
<?php
/**
* @file
* Contains \Drupal\telephone\Plugin\Field\FieldWidget\TelephoneDefaultWidget.
*/
namespace Drupal\telephone\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'telephone_default' widget.
*
* @FieldWidget(
* id = "telephone_default",
* label = @Translation("Telephone number"),
* field_types = {
* "telephone"
* }
* )
*/
class TelephoneDefaultWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'placeholder' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $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;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$placeholder = $this->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder));
}
else {
$summary[] = t('No placeholder');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + array(
'#type' => 'tel',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#placeholder' => $this->getSetting('placeholder'),
);
return $element;
}
}

View file

@ -0,0 +1,104 @@
<?php
/**
* @file
* Contains \Drupal\telephone\Tests\TelephoneFieldTest.
*/
namespace Drupal\telephone\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests the creation of telephone fields.
*
* @group telephone
*/
class TelephoneFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array(
'field',
'node',
'telephone'
);
/**
* A user with permission to create articles.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'article'));
$this->webUser = $this->drupalCreateUser(array('create article content', 'edit own article content'));
$this->drupalLogin($this->webUser);
}
// Test fields.
/**
* Helper function for testTelephoneField().
*/
function testTelephoneField() {
// Add the telephone field to the article content type.
entity_create('field_storage_config', array(
'field_name' => 'field_telephone',
'entity_type' => 'node',
'type' => 'telephone',
))->save();
entity_create('field_config', array(
'field_name' => 'field_telephone',
'label' => 'Telephone Number',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
entity_get_form_display('node', 'article', 'default')
->setComponent('field_telephone', array(
'type' => 'telephone_default',
'settings' => array(
'placeholder' => '123-456-7890',
),
))
->save();
entity_get_display('node', 'article', 'default')
->setComponent('field_telephone', array(
'type' => 'telephone_link',
'weight' => 1,
))
->save();
// Display creation form.
$this->drupalGet('node/add/article');
$this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
$this->assertRaw('placeholder="123-456-7890"');
// Test basic entry of telephone field.
$edit = array(
'title[0][value]' => $this->randomMachineName(),
'field_telephone[0][value]' => "123456789",
);
$this->drupalPostForm(NULL, $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 on output.
$edit = array(
'title[0][value]' => $this->randomMachineName(),
'field_telephone[0][value]' => "1234 56789",
);
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertRaw('<a href="tel:123456789">', 'Telephone link is output with whitespace removed.');
}
}

View file

@ -0,0 +1,79 @@
<?php
/**
* @file
* Contains \Drupal\telephone\Tests\TelephoneItemTest.
*/
namespace Drupal\telephone\Tests;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
/**
* Tests the new entity API for the telephone field type.
*
* @group telephone
*/
class TelephoneItemTest extends FieldUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('telephone');
protected function setUp() {
parent::setUp();
// Create a telephone field storage and field for validation.
entity_create('field_storage_config', array(
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'telephone',
))->save();
entity_create('field_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
))->save();
}
/**
* Tests using entity fields of the telephone field type.
*/
public function testTestItem() {
// Verify entity creation.
$entity = entity_create('entity_test');
$value = '+0123456789';
$entity->field_test = $value;
$entity->name->value = $this->randomMachineName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test->value, $value);
$this->assertEqual($entity->field_test[0]->value, $value);
// Verify changing the field value.
$new_value = '+41' . rand(1000000, 9999999);
$entity->field_test->value = $new_value;
$this->assertEqual($entity->field_test->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_test->value, $new_value);
// Test sample item generation.
$entity = entity_create('entity_test');
$entity->field_test->generateSampleItems();
$this->entityValidateAndSave($entity);
}
}

View file

@ -0,0 +1,8 @@
name: Telephone
type: module
description: 'Defines a field type for telephone numbers.'
package: Field types
version: VERSION
core: 8.x
dependencies:
- field

View file

@ -0,0 +1,35 @@
<?php
/**
* @file
* Defines a simple telephone number field type.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function telephone_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.telephone':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Telephone module allows you to create fields that contain telephone numbers. See the <a href="!field">Field module help</a> and the <a href="!field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href="!telephone_documentation">online documentation for the Telephone module</a>.', array('!field' => \Drupal::url('help.page', array('name' => 'field')), '!field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#', '!telephone_documentation' => 'https://www.drupal.org/documentation/modules/telephone')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing and displaying telephone fields') . '</dt>';
$output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the telephone field can be configured separately. See the <a href="!field_ui">Field UI help</a> for more information on how to manage fields and their display.', array('!field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#')) . '</dd>';
$output .= '<dt>' . t('Displaying telephone numbers as links') . '</dt>';
$output .= '<dd>' . t('Telephone numbers can be displayed as links with the scheme name <em>tel:</em> by choosing the <em>Telephone</em> display format on the <em>Manage display</em> page. Any spaces will be stripped out of the link text. This semantic markup improves the user experience on mobile and assistive technology devices.') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function telephone_field_formatter_info_alter(&$info) {
$info['string']['field_types'][] = 'telephone';
}