summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcatch2012-11-04 21:56:28 (GMT)
committercatch2012-11-04 21:56:28 (GMT)
commit00613c90093976450b30055a5fca7bb8c4a531a6 (patch)
treed8a83a5c316e6aa2fb9de2af9e56d643fa906000
parent6a56a907d3826b349635cf0469f91c45df37674c (diff)
Issue #1770172 by netsensei, webflo, sun, linclark: Convert email module widgets and formatters to Plugin system.
-rw-r--r--core/modules/field/modules/email/email.info1
-rw-r--r--core/modules/field/modules/email/email.module62
-rw-r--r--core/modules/field/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php46
-rw-r--r--core/modules/field/modules/email/lib/Drupal/email/Plugin/field/widget/EmailDefaultWidget.php39
4 files changed, 96 insertions, 52 deletions
diff --git a/core/modules/field/modules/email/email.info b/core/modules/field/modules/email/email.info
index 5c3d3ff..c5b7437 100644
--- a/core/modules/field/modules/email/email.info
+++ b/core/modules/field/modules/email/email.info
@@ -4,4 +4,3 @@ package = Core
version = VERSION
core = 8.x
dependencies[] = field
-dependencies[] = text
diff --git a/core/modules/field/modules/email/email.module b/core/modules/field/modules/email/email.module
index 6d955d6..70d47dd 100644
--- a/core/modules/field/modules/email/email.module
+++ b/core/modules/field/modules/email/email.module
@@ -27,72 +27,32 @@ function email_field_info() {
'label' => t('E-mail'),
'description' => t('This field stores an e-mail address in the database.'),
'default_widget' => 'email_default',
- 'default_formatter' => 'text_plain',
+ 'default_formatter' => 'email_mailto',
),
);
}
/**
- * Implements hook_field_is_empty().
- */
-function email_field_is_empty($item, $field) {
- return !isset($item['value']) || $item['value'] === '';
-}
-
-/**
- * Implements hook_field_widget_info().
- */
-function email_field_widget_info() {
- return array(
- 'email_default' => array(
- 'label' => t('E-mail'),
- 'field types' => array('email'),
- ),
- );
-}
-
-/**
- * Implements hook_field_widget_form().
+ * Implements hook_field_info_alter().
*/
-function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
- $element['value'] = $element + array(
- '#type' => 'email',
- '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
- );
- return $element;
-
+function email_field_info_alter(&$info) {
+ if (module_exists('text')) {
+ $info['email']['default_formatter'] = 'text_plain';
+ }
}
/**
- * Implements hook_field_formatter_info().
+ * Implements hook_field_is_empty().
*/
-function email_field_formatter_info() {
- return array(
- 'email_mailto' => array(
- 'label' => t('Mailto link'),
- 'field types' => array('email'),
- ),
- );
+function email_field_is_empty($item, $field) {
+ return !isset($item['value']) || $item['value'] === '';
}
/**
* Implements hook_field_formatter_info_alter().
*/
function email_field_formatter_info_alter(&$info) {
- $info['text_plain']['field types'][] = 'email';
-}
-
-/**
- * Implements hook_field_formatter_view().
- */
-function email_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
- foreach ($items as $delta => $item) {
- $element[$delta] = array(
- '#type' => 'link',
- '#title' => $item['value'],
- '#href' => 'mailto:' . $item['value'],
- );
+ if (isset($info['text_plain'])) {
+ $info['text_plain']['field_types'][] = 'email';
}
- return $element;
}
diff --git a/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php b/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
new file mode 100644
index 0000000..388acd9
--- /dev/null
+++ b/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\email\Plugin\field\formatter\MailToFormatter.
+ */
+
+namespace Drupal\email\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 'email_mailto' formatter.
+ *
+ * @Plugin(
+ * id = "email_mailto",
+ * module = "email",
+ * label = @Translation("Email"),
+ * field_types = {
+ * "email"
+ * }
+ * )
+ */
+class MailToFormatter 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;
+ }
+
+}
diff --git a/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/widget/EmailDefaultWidget.php b/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/widget/EmailDefaultWidget.php
new file mode 100644
index 0000000..bb4e859
--- /dev/null
+++ b/core/modules/field/modules/email/lib/Drupal/email/Plugin/field/widget/EmailDefaultWidget.php
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\email\Plugin\field\widget\EmailDefaultWidget.
+ */
+
+namespace Drupal\email\Plugin\field\widget;
+
+use Drupal\Core\Annotation\Plugin;
+use Drupal\Core\Annotation\Translation;
+use Drupal\field\Plugin\Type\Widget\WidgetBase;
+
+/**
+ * Plugin implementation of the 'email_default' widget.
+ *
+ * @Plugin(
+ * id = "email_default",
+ * module = "email",
+ * label = @Translation("E-mail"),
+ * field_types = {
+ * "email"
+ * }
+ * )
+ */
+class EmailDefaultWidget 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' => 'email',
+ '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
+ );
+ return $element;
+ }
+
+}