Skip to content

Commit

Permalink
Merge branch '4.x' into tv4g1-issue1859-sequence-coordinates-field
Browse files Browse the repository at this point in the history
  • Loading branch information
laceysanderson authored Apr 28, 2024
2 parents e7ac633 + 85d19d9 commit 39f4c5e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
7 changes: 7 additions & 0 deletions tripal/config/schema/tripal.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,10 @@ field.formatter.settings.*:
type: integer
label: 'Word wrap setting'
nullable: true

field.widget.settings.*:
type: mapping
mapping:
filter_format:
type: string
label: 'Filter format for a filtered text field'
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,27 @@ class DefaultTripalTextTypeFormatter extends TripalFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];

// Default filter format.
$filter_format = 'basic_html';

// We need to get the format which was set in the widget settings
// because they need to match.
$entity_type = $this->fieldDefinition->get('entity_type');
$bundle = $this->fieldDefinition->get('bundle');
$field_name = $this->fieldDefinition->get('field_name');
$form_display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $bundle);
$widget = $form_display->getComponent($field_name);
if (array_key_exists('filter_format', $widget['settings'])) {
$filter_format = $widget['settings']['filter_format'];
}

foreach($items as $delta => $item) {
$value_string = $item->get('value')->getValue();
$elements[$delta] = [
"#markup" => $item->get("value")->getString(),
'#type' => 'processed_text',
'#text' => $value_string,
'#format' => $filter_format,
'#langcode' => $item->getLangcode(),
];
}

Expand Down
88 changes: 87 additions & 1 deletion tripal/src/Plugin/Field/FieldWidget/TripalTextTypeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,98 @@ class TripalTextTypeWidget extends TripalWidgetBase {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

$element['value'] = $element + [
'#type' => 'textarea',
'#base_type' => 'textarea',
'#type' => 'text_format',
'#format' => $this->getSetting('filter_format'),
'#default_value' => $items[$delta]->value ?? '',
'#placeholder' => $this->getSetting('placeholder'),
'#attributes' => ['class' => ['js-text-full', 'text-full']],
];

return $element;
}

/**
* {@inheritdoc}
*/
public static function afterBuild(array $element, FormStateInterface $form_state) {
parent::afterBuild($element, $form_state);

// Alter the format drop down so that it is hidden.
// We do this because any changes here are not actually saved and thus
// having it enabled is misleading.
// Note: We couldn't disable it for the text format element would stop working ;-)
foreach(\Drupal\Core\Render\Element::children($element) as $key) {
$element[$key]['value']['format']['#attributes']['class'][] = 'hidden';
}

return $element;
}

/**
* {@inheritDoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

// The text_format element returns an item consisting of both a value and a
// format. We only want to keep the format.
foreach ($values as $key => $item) {
$values[$key]['value'] = $item['value']['value'];
}
return $values;
}

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'filter_format' => 'basic_html',
] + parent::defaultSettings();
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {

// Get all the filter formats available for the current site.
$options = [];
foreach (filter_formats() as $name => $object) {
$options[$name] = $object->get('name');
}

$element['filter_format'] = [
'#type' => 'select',
'#title' => $this->t('Text Filter Format'),
'#options' => $options,
'#description' => $this->t("Select the text filter format you want applied
to this field. Everyone will use the same format. If a user does not have
permission to the format chosen for this field then they won't be able to
edit it. Please keep in mind there are security concerns with choosing
'full_html' and thus this should only be your choice if you have
restricted all people able to edit this field to those you trust."),
'#default_value' => $this->getSetting('filter_format'),
'#required' => TRUE,
];

return $element;
}

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];

$format = $this->getSetting('filter_format');
$all_formats = filter_formats();
$format_label = $all_formats[$format]->get('name');

$summary[] = $this->t("Text Format: @format", ['@format' => $format_label]);

return $summary;
}
}

0 comments on commit 39f4c5e

Please sign in to comment.