Skip to content
Snippets Groups Projects
Commit 481500b7 authored by Tasneem Natshah's avatar Tasneem Natshah
Browse files

Issue #3296335 by Tasneem Natshah: Finalize the Gherkin Script field type...

Issue #3296335 by Tasneem Natshah: Finalize the Gherkin Script field type widget and formatter with options to read from a list of step definitions
parent 07aa39fd
No related branches found
No related tags found
No related merge requests found
name: 'Gherkin Editor'
name: 'Gherkin Script'
type: module
description: 'Gherkin editing interface.'
package: Other
......
<?php
namespace Drupal\gherkin\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* @FieldFormatter(
* id = "gherkin_default_formatter",
* module = "gherkin",
* label = @Translation("Gherkin"),
* field_types = {
* "gherkin_script"
* }
* )
*/
class GherkinDefaultFormatter extends FormatterBase{
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
// Render each element as markup.
$element[$delta] = ['#markup' => $item->value];
}
return $elements;
}
}
\ No newline at end of file
<?php
namespace Drupal\gherkin\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* @FieldType(
* id = "gherkin_script",
* module = "gherkin",
* label = @Translation("Gherkin Script"),
* default_formatter = "gherkin_default_formatter",
* default_widget = "gherkin_default_widget"
* )
*/
class GherkinItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'text',
'size' => 'medium',
'not null' => FALSE,
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition){
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Gherkin Script'));
}
}
\ No newline at end of file
<?php
namespace Drupal\gherkin\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* @FieldWidget(
* id = "gherkin_default_widget",
* module = "gherkin",
* label = @Translation("Gherkin default"),
* field_types = {
* "gherkin_script"
* }
* )
*/
class GherkinDefaultwidget extends widgetBase{
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$value = isset($items[$delta]->value) ? $items[$delta]->value : '';
$element += [
'#type' => 'text_format',
'#format' => 'full_html',
'#default_value' => $value,
];
return ['value' => $element];
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment