Skip to content

Commit b6c1765

Browse files
committed
Initial D8 port commit.
0 parents  commit b6c1765

File tree

10 files changed

+330
-0
lines changed

10 files changed

+330
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
field.field.*.*.*.third_party.list_predefined_options:
2+
type: mapping
3+
label: Predefined options
4+
mapping:
5+
plugin_id:
6+
type: string
7+
label: Plugin ID

list_predefined_options.info.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'List Predefined Options'
2+
type: module
3+
description: 'Provides reusable, predefined options for list fields'
4+
package: Fields
5+
dependencies:
6+
- options
7+
core: 8.x

list_predefined_options.module

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides reusable, predefined options for list fields.
6+
*/
7+
8+
use Drupal\Core\Form\FormStateInterface;
9+
use Drupal\Core\Entity\FieldableEntityInterface;
10+
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11+
12+
/**
13+
* Implements hook_form_FORM_ID_alter(): field_storage_config_edit_form.
14+
*/
15+
function list_predefined_options_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
16+
/** @var \Drupal\Core\Field\FieldConfigInterface $field */
17+
$field = $form_state->getFormObject()->getEntity();
18+
19+
$field_types = [
20+
'list_float',
21+
'list_integer',
22+
'list_string',
23+
];
24+
25+
if (in_array($field->getType(), $field_types)) {
26+
$options = \Drupal::service('plugin.manager.list_options.processor')->listOptions();
27+
$options = ['' => t('Custom')] + $options;
28+
29+
$settings = $field->getThirdPartySettings('list_predefined_options');
30+
$form['list_predefined_options_plugin_id'] = [
31+
'#type' => 'select',
32+
'#title' => t('Allowed values'),
33+
'#options' => $options,
34+
'#default_value' => isset($settings['plugin_id']) ? $settings['plugin_id'] : NULL,
35+
'#weight' => -20,
36+
];
37+
// TODO: currently produces an error.
38+
//$form['allowed_values']['#states']['visible'][':input[name="field[settings][allowed_values_function]"]'] = array('value' => '');
39+
$form['allowed_values']['#access'] = TRUE;
40+
$form['allowed_values_function_display']['#access'] = FALSE;
41+
42+
$form['#entity_builders'][] = 'list_predefined_options_form_field_storage_config_edit_form_builder';
43+
}
44+
}
45+
46+
/**
47+
* Entity builder callback to save our settings into the field storage config.
48+
*/
49+
function list_predefined_options_form_field_storage_config_edit_form_builder($entity_type, $entity, &$form, FormStateInterface $form_state) {
50+
$plugin_id = $form_state->getValue('list_predefined_options_plugin_id');
51+
if (!empty($plugin_id)) {
52+
$entity->setThirdPartySetting('list_predefined_options', 'plugin_id', $plugin_id);
53+
$entity->setSetting('allowed_values_function', 'list_predefined_options_allowed_values');
54+
}
55+
else {
56+
$entity->unsetThirdPartySetting('list_predefined_options', 'plugin_id');
57+
// If one of our plugins is being removed, remove our allowed values
58+
// function setting.
59+
if ($entity->getSetting('allowed_values_function') == 'list_predefined_options_allowed_values') {
60+
$entity->setSetting('allowed_values_function', '');
61+
}
62+
}
63+
}
64+
65+
/**
66+
* Implements callback_allowed_values_function().
67+
*/
68+
function list_predefined_options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
69+
$plugin_id = $definition->getThirdPartySetting('list_predefined_options', 'plugin_id');
70+
$plugin = \Drupal::service('plugin.manager.list_options.processor')->createInstance($plugin_id);
71+
return $plugin->getListOptions($definition, $entity, $cacheable);
72+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
plugin.manager.list_options.processor:
3+
class: Drupal\list_predefined_options\Plugin\ListOptionsManager
4+
parent: default_plugin_manager

src/Annotation/ListOptions.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\list_predefined_options\Annotation\ListOptions.
6+
*/
7+
8+
namespace Drupal\list_predefined_options\Annotation;
9+
10+
use Drupal\Component\Annotation\Plugin;
11+
12+
/**
13+
* Defines a List options item annotation object.
14+
*
15+
* @see \Drupal\list_predefined_options\Plugin\ListOptionsManager
16+
* @see plugin_api
17+
*
18+
* @Annotation
19+
*/
20+
class ListOptions extends Plugin {
21+
22+
/**
23+
* The plugin ID.
24+
*
25+
* @var string
26+
*/
27+
public $id;
28+
29+
/**
30+
* The label of the plugin.
31+
*
32+
* @var \Drupal\Core\Annotation\Translation
33+
*
34+
* @ingroup plugin_translatable
35+
*/
36+
public $label;
37+
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Drupal\list_predefined_options\Plugin\ListOptions;
4+
5+
use Drupal\list_predefined_options\Plugin\ListOptionsBase;
6+
use Drupal\Core\Entity\FieldableEntityInterface;
7+
use Drupal\Core\Field\FieldStorageDefinitionInterface;
8+
9+
/**
10+
* @ListOptions(
11+
* id = "timezones",
12+
* label = @Translation("Timezones"),
13+
* )
14+
*/
15+
class Timezones extends ListOptionsBase {
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function getListOptions(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
21+
return system_time_zones();
22+
}
23+
24+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Drupal\list_predefined_options\Plugin\ListOptions;
4+
5+
use Drupal\list_predefined_options\Plugin\ListOptionsBase;
6+
use Drupal\Core\Entity\FieldableEntityInterface;
7+
use Drupal\Core\Field\FieldStorageDefinitionInterface;
8+
9+
/**
10+
* @ListOptions(
11+
* id = "us_states",
12+
* label = @Translation("US States"),
13+
* )
14+
*/
15+
class USStates extends ListOptionsBase {
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function getListOptions(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
21+
$options['AL'] = t('Alabama');
22+
$options['AK'] = t('Alaska');
23+
$options['AZ'] = t('Arizona');
24+
$options['AR'] = t('Arkansas');
25+
$options['CA'] = t('California');
26+
$options['CO'] = t('Colorado');
27+
$options['CT'] = t('Connecticut');
28+
$options['DE'] = t('Delaware');
29+
$options['DC'] = t('District Of Columbia');
30+
$options['FL'] = t('Florida');
31+
$options['GA'] = t('Georgia');
32+
$options['HI'] = t('Hawaii');
33+
$options['ID'] = t('Idaho');
34+
$options['IL'] = t('Illinois');
35+
$options['IN'] = t('Indiana');
36+
$options['IA'] = t('Iowa');
37+
$options['KS'] = t('Kansas');
38+
$options['KY'] = t('Kentucky');
39+
$options['LA'] = t('Louisiana');
40+
$options['ME'] = t('Maine');
41+
$options['MD'] = t('Maryland');
42+
$options['MA'] = t('Massachusetts');
43+
$options['MI'] = t('Michigan');
44+
$options['MN'] = t('Minnesota');
45+
$options['MS'] = t('Mississippi');
46+
$options['MO'] = t('Missouri');
47+
$options['MT'] = t('Montana');
48+
$options['NE'] = t('Nebraska');
49+
$options['NV'] = t('Nevada');
50+
$options['NH'] = t('New Hampshire');
51+
$options['NJ'] = t('New Jersey');
52+
$options['NM'] = t('New Mexico');
53+
$options['NY'] = t('New York');
54+
$options['NC'] = t('North Carolina');
55+
$options['ND'] = t('North Dakota');
56+
$options['OH'] = t('Ohio');
57+
$options['OK'] = t('Oklahoma');
58+
$options['OR'] = t('Oregon');
59+
$options['PA'] = t('Pennsylvania');
60+
$options['RI'] = t('Rhode Island');
61+
$options['SC'] = t('South Carolina');
62+
$options['SD'] = t('South Dakota');
63+
$options['TN'] = t('Tennessee');
64+
$options['TX'] = t('Texas');
65+
$options['UT'] = t('Utah');
66+
$options['VT'] = t('Vermont');
67+
$options['VA'] = t('Virginia');
68+
$options['WA'] = t('Washington');
69+
$options['WV'] = t('West Virginia');
70+
$options['WI'] = t('Wisconsin');
71+
$options['WY'] = t('Wyoming');
72+
return $options;
73+
}
74+
75+
}

src/Plugin/ListOptionsBase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Drupal\list_predefined_options\Plugin;
4+
5+
use Drupal\Component\Plugin\PluginBase;
6+
7+
/**
8+
* Base class for List options plugins.
9+
*/
10+
abstract class ListOptionsBase extends PluginBase implements ListOptionsInterface {
11+
12+
// Add common methods and abstract methods for your plugin type here.
13+
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\list_predefined_options\Plugin;
4+
5+
use Drupal\Component\Plugin\PluginInspectionInterface;
6+
use Drupal\Core\Entity\FieldableEntityInterface;
7+
use Drupal\Core\Field\FieldStorageDefinitionInterface;
8+
9+
/**
10+
* Defines an interface for List options plugins.
11+
*/
12+
interface ListOptionsInterface extends PluginInspectionInterface {
13+
14+
/**
15+
* Returns the list of options.
16+
*
17+
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
18+
* The field storage definition.
19+
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
20+
* (optional) The entity context if known, or NULL if the allowed values are
21+
* being collected without the context of a specific entity.
22+
* @param bool &$cacheable
23+
* (optional) If an $entity is provided, the $cacheable parameter should be
24+
* modified by reference and set to FALSE if the set of allowed values
25+
* returned was specifically adjusted for that entity and cannot not be reused
26+
* for other entities. Defaults to TRUE.
27+
*
28+
* @return array
29+
* The array of allowed values. Keys of the array are the raw stored values
30+
* (number or text), values of the array are the display labels. If $entity
31+
* is NULL, you should return the list of all the possible allowed values in
32+
* any context so that other code (e.g. Views filters) can support the allowed
33+
* values for all possible entities and bundles.
34+
*/
35+
public function getListOptions(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE);
36+
37+
}

src/Plugin/ListOptionsManager.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\list_predefined_options\Plugin\ListOptionsManager.
6+
*/
7+
8+
namespace Drupal\list_predefined_options\Plugin;
9+
10+
use Drupal\Core\Plugin\DefaultPluginManager;
11+
use Drupal\Core\Cache\CacheBackendInterface;
12+
use Drupal\Core\Extension\ModuleHandlerInterface;
13+
14+
/**
15+
* Provides the List options plugin manager.
16+
*/
17+
class ListOptionsManager extends DefaultPluginManager {
18+
19+
/**
20+
* Constructor for ListOptionsManager objects.
21+
*
22+
* @param \Traversable $namespaces
23+
* An object that implements \Traversable which contains the root paths
24+
* keyed by the corresponding namespace to look for plugin implementations.
25+
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
26+
* Cache backend instance to use.
27+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
28+
* The module handler to invoke the alter hook with.
29+
*/
30+
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
31+
parent::__construct('Plugin/ListOptions', $namespaces, $module_handler, 'Drupal\list_predefined_options\Plugin\ListOptionsInterface', 'Drupal\list_predefined_options\Annotation\ListOptions');
32+
33+
$this->alterInfo('list_predefined_options_list_options_info');
34+
$this->setCacheBackend($cache_backend, 'list_predefined_options_list_options_plugins');
35+
}
36+
37+
/**
38+
* Returns a list of names available predefined list options.
39+
*
40+
* @return
41+
* An array keyed by plugin ID whose values are the plugin labels.
42+
*/
43+
public function listOptions() {
44+
$options = array();
45+
foreach ($this->getDefinitions() as $key => $definition) {
46+
$options[$key] = $definition['label'];
47+
}
48+
natcasesort($options);
49+
return $options;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)