Issue #1787238 by aspilicious, swentel, yched | pcambra: Convert options module formatters to Plugin system.
parent
316b5edf9a
commit
e9aa9c4ac4
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\options\Plugin\field\formatter\OptionsDefaultFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\options\Plugin\field\formatter;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'list_default' formatter.
|
||||
*
|
||||
* @Plugin(
|
||||
* id = "options_list_default",
|
||||
* module = "options",
|
||||
* label = @Translation("Default"),
|
||||
* field_types = {
|
||||
* "list_integer",
|
||||
* "list_float",
|
||||
* "list_text",
|
||||
* "list_boolean"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class OptionsDefaultFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
||||
$elements = array();
|
||||
|
||||
$allowed_values = options_allowed_values($this->field, $this->instance, $entity);
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
if (isset($allowed_values[$item['value']])) {
|
||||
$output = field_filter_xss($allowed_values[$item['value']]);
|
||||
}
|
||||
else {
|
||||
// If no match was found in allowed values, fall back to the key.
|
||||
$output = field_filter_xss($item['value']);
|
||||
}
|
||||
$elements[$delta] = array('#markup' => $output);
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\options\Plugin\field\formatter\OptionsKeyFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\options\Plugin\field\formatter;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'list_key' formatter.
|
||||
*
|
||||
* @Plugin(
|
||||
* id = "options_list_key",
|
||||
* module = "options",
|
||||
* label = @Translation("Key"),
|
||||
* field_types = {
|
||||
* "list_integer",
|
||||
* "list_float",
|
||||
* "list_text",
|
||||
* "list_boolean"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class OptionsKeyFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
||||
$elements = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$elements[$delta] = array('#markup' => field_filter_xss($item['value']));
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Definition of Drupal\options\Tests\OptionsFieldUITest.
|
||||
* Contains \Drupal\options\Tests\OptionsFieldUITest.
|
||||
*/
|
||||
|
||||
namespace Drupal\options\Tests;
|
||||
|
@ -21,6 +21,13 @@ class OptionsFieldUITest extends FieldTestBase {
|
|||
*/
|
||||
public static $modules = array('options', 'field_test', 'taxonomy', 'field_ui');
|
||||
|
||||
/**
|
||||
* The name of the created content type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type_name;
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Options field UI',
|
||||
|
@ -33,11 +40,12 @@ class OptionsFieldUITest extends FieldTestBase {
|
|||
parent::setUp();
|
||||
|
||||
// Create test user.
|
||||
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer taxonomy'));
|
||||
$admin_user = $this->drupalCreateUser(array('access content', 'administer taxonomy', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'bypass node access', 'administer node fields', 'administer node display'));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Create content type, with underscores.
|
||||
$type_name = 'test_' . strtolower($this->randomName());
|
||||
$this->type_name = $type_name;
|
||||
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
|
||||
$this->type = $type->type;
|
||||
}
|
||||
|
@ -286,4 +294,51 @@ class OptionsFieldUITest extends FieldTestBase {
|
|||
$this->assertIdentical($field['settings']['allowed_values'], $result, $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests normal and key formatter display on node display.
|
||||
*/
|
||||
function testNodeDisplay() {
|
||||
$this->field_name = strtolower($this->randomName());
|
||||
$this->createOptionsField('list_boolean');
|
||||
$node = $this->drupalCreateNode(array('type' => $this->type));
|
||||
|
||||
$on = $this->randomName();
|
||||
$off = $this->randomName();
|
||||
$allowed_values = array(1 => $on, 0 => $off);
|
||||
$edit = array(
|
||||
'on' => $on,
|
||||
'off' => $off,
|
||||
);
|
||||
|
||||
$this->drupalPost($this->admin_path, $edit, t('Save field settings'));
|
||||
$this->assertText(format_string('Updated field !field_name field settings.', array('!field_name' => $this->field_name)), "The 'On' and 'Off' form fields work for boolean fields.");
|
||||
|
||||
// Select a default value.
|
||||
$edit = array(
|
||||
$this->field_name . '[und]' => '1',
|
||||
);
|
||||
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save and keep published'));
|
||||
|
||||
// Check the node page and see if the values are correct.
|
||||
$file_formatters = array('options_list_default', 'options_list_key');
|
||||
foreach ($file_formatters as $formatter) {
|
||||
$edit = array(
|
||||
"fields[$this->field_name][type]" => $formatter,
|
||||
);
|
||||
$this->drupalPost('admin/structure/types/manage/' . $this->type_name . '/display', $edit, t('Save'));
|
||||
$this->drupalGet('node/' . $node->nid);
|
||||
|
||||
if ($formatter == 'options_list_default') {
|
||||
$output = $on;
|
||||
}
|
||||
else {
|
||||
$output = '1';
|
||||
}
|
||||
|
||||
$elements = $this->xpath('//div[text()="' . $output . '"]');
|
||||
$this->assertEqual(count($elements), 1, 'Correct options found.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -866,50 +866,3 @@ function theme_options_none($variables) {
|
|||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_info().
|
||||
*/
|
||||
function options_field_formatter_info() {
|
||||
return array(
|
||||
'list_default' => array(
|
||||
'label' => t('Default'),
|
||||
'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
|
||||
),
|
||||
'list_key' => array(
|
||||
'label' => t('Key'),
|
||||
'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_view().
|
||||
*/
|
||||
function options_field_formatter_view(EntityInterface $entity, $field, $instance, $langcode, $items, $display) {
|
||||
$element = array();
|
||||
|
||||
switch ($display['type']) {
|
||||
case 'list_default':
|
||||
$allowed_values = options_allowed_values($field, $instance, $entity);
|
||||
foreach ($items as $delta => $item) {
|
||||
if (isset($allowed_values[$item['value']])) {
|
||||
$output = field_filter_xss($allowed_values[$item['value']]);
|
||||
}
|
||||
else {
|
||||
// If no match was found in allowed values, fall back to the key.
|
||||
$output = field_filter_xss($item['value']);
|
||||
}
|
||||
$element[$delta] = array('#markup' => $output);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'list_key':
|
||||
foreach ($items as $delta => $item) {
|
||||
$element[$delta] = array('#markup' => field_filter_xss($item['value']));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue