diff --git a/field_example/config/schema/field_example.schema.yml b/field_example/config/schema/field_example.schema.yml new file mode 100644 index 0000000..6b63036 --- /dev/null +++ b/field_example/config/schema/field_example.schema.yml @@ -0,0 +1,10 @@ +field.field_example_rgb.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + value: + type: string + label: 'Value' diff --git a/field_example/field_example.info.yml b/field_example/field_example.info.yml new file mode 100644 index 0000000..2ba15b6 --- /dev/null +++ b/field_example/field_example.info.yml @@ -0,0 +1,5 @@ +name: Field Example +type: module +description: An implementation of a field to show the Field API +package: Example modules +core: 8.x diff --git a/field_example/field_example.js b/field_example/field_example.js new file mode 100644 index 0000000..58e5339 --- /dev/null +++ b/field_example/field_example.js @@ -0,0 +1,24 @@ +/** + * @file + * Javascript for Field Example. + */ + +/** + * Provides a farbtastic colorpicker for the fancier widget. + */ +(function($) { + Drupal.behaviors.field_example_colorpicker = { + attach: function() { + $(".edit-field-example-colorpicker").on("focus", function(event) { + var edit_field = this; + var picker = $(this).closest('div').parent().find(".field-example-colorpicker"); + // Hide all color pickers except this one. + $(".field-example-colorpicker").hide(); + $(picker).show(); + $.farbtastic(picker, function(color) { + edit_field.value = color; + }).setColor(edit_field.value); + }); + } + }; +})(jQuery); diff --git a/field_example/field_example.module b/field_example/field_example.module new file mode 100644 index 0000000..1f7e53c --- /dev/null +++ b/field_example/field_example.module @@ -0,0 +1,107 @@ + 'string', + 'label' => t('Hex value'), + ); + } + return static::$propertyDefinitions; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldInterface $field) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'text', + 'size' => 'tiny', + ), + ), + ); + } + +} diff --git a/field_example/lib/Drupal/field_example/Plugin/field/formatter/ColorBackgroudFormatter.php b/field_example/lib/Drupal/field_example/Plugin/field/formatter/ColorBackgroudFormatter.php new file mode 100644 index 0000000..0c1645d --- /dev/null +++ b/field_example/lib/Drupal/field_example/Plugin/field/formatter/ColorBackgroudFormatter.php @@ -0,0 +1,54 @@ + $item) { + $elements[$delta] = array( + '#type' => 'html_tag', + '#tag' => 'p', + '#value' => t('The content area color has been changed to @code', array('@code' => $item->value)), + '#attached' => array( + 'css' => array( + array( + 'data' => 'main { background-color:' . $item->value . ';}', + 'type' => 'inline', + ), + ), + ), + ); + } + + return $elements; + } + +} diff --git a/field_example/lib/Drupal/field_example/Plugin/field/formatter/SimpleTextFormatter.php b/field_example/lib/Drupal/field_example/Plugin/field/formatter/SimpleTextFormatter.php new file mode 100644 index 0000000..0c771c5 --- /dev/null +++ b/field_example/lib/Drupal/field_example/Plugin/field/formatter/SimpleTextFormatter.php @@ -0,0 +1,52 @@ + $item) { + $elements[$delta] = array( + // We create a render array to produce the desired markup, + // "

The color code ... #hexcolor

". + // See theme_html_tag(). + '#type' => 'html_tag', + '#tag' => 'p', + '#attributes' => array( + 'style' => 'color: ' . $item->value, + ), + '#value' => t('The color code in this field is @code', array('@code' => $item->value)), + ); + } + + return $elements; + } + +} diff --git a/field_example/lib/Drupal/field_example/Plugin/field/widget/ColorPickerWidget.php b/field_example/lib/Drupal/field_example/Plugin/field/widget/ColorPickerWidget.php new file mode 100644 index 0000000..27b3aac --- /dev/null +++ b/field_example/lib/Drupal/field_example/Plugin/field/widget/ColorPickerWidget.php @@ -0,0 +1,48 @@ + '
', + '#attributes' => array('class' => array('edit-field-example-colorpicker')), + '#attached' => array( + // Add Farbtastic color picker. + 'library' => array( + array('system', 'jquery.farbtastic'), + ), + // Add javascript to trigger the colorpicker. + 'js' => array(drupal_get_path('module', 'field_example') . '/field_example.js'), + ), + ); + return $element; + } + +} diff --git a/field_example/lib/Drupal/field_example/Plugin/field/widget/Text3Widget.php b/field_example/lib/Drupal/field_example/Plugin/field/widget/Text3Widget.php new file mode 100644 index 0000000..9ff0e86 --- /dev/null +++ b/field_example/lib/Drupal/field_example/Plugin/field/widget/Text3Widget.php @@ -0,0 +1,66 @@ +value) ? $items[$delta]->value : ''; + // Parse the single hex string into RBG values. + if (!empty($value)) { + preg_match_all('@..@', substr($value, 1), $match); + } + else { + $match = array(array()); + } + + // Set up the form element for this widget. + $element += array( + '#type' => 'fieldset', + '#element_validate' => array('field_example_3text_validate'), + ); + + // Add in the RGB textfield elements. + foreach (array('r' => t('Red'), 'g' => t('Green'), 'b' => t('Blue')) as $key => $title) { + $element[$key] = array( + '#type' => 'textfield', + '#title' => $title, + '#size' => 2, + '#default_value' => array_shift($match[0]), + '#attributes' => array('class' => array('rgb-entry')), + '#description' => t('The 2-digit hexadecimal representation of @color saturation, like "a1" or "ff"', array('@color' => $title)), + ); + // Since Form API doesn't allow a fieldset to be required, we + // have to require each field element individually. + if ($element['#required']) { + $element[$key]['#required'] = TRUE; + } + } + return $element; + } + +} diff --git a/field_example/lib/Drupal/field_example/Plugin/field/widget/TextWidget.php b/field_example/lib/Drupal/field_example/Plugin/field/widget/TextWidget.php new file mode 100644 index 0000000..1f2fb6f --- /dev/null +++ b/field_example/lib/Drupal/field_example/Plugin/field/widget/TextWidget.php @@ -0,0 +1,45 @@ +value) ? $items[$delta]->value : ''; + $element += array( + '#type' => 'textfield', + '#default_value' => $value, + // Allow a slightly larger size that the field length to allow for some + // configurations where all characters won't fit in input field. + '#size' => 7, + '#maxlength' => 7, + '#element_validate' => array('field_example_text_validate'), + ); + return $element; + } + +}