Skip to content
Snippets Groups Projects
Commit 032be677 authored by Nhat Tran's avatar Nhat Tran
Browse files

Adapt to Drupal changes

parent ea3fdcb1
No related branches found
No related tags found
No related merge requests found
......@@ -6,18 +6,82 @@
*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\filter\Plugin\Filter;
/**
* Implements hook_field_widget_form_alter()
*/
// function advanced_text_formatter_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
// var_dump($element, $context['widget']);
// }
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_string_textfield_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textfield_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textarea_form_alter(&$element, &$form_state, $context) {
function advanced_text_formatter_field_widget_string_textarea_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textarea_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_long_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textarea_with_summary_form_alter(&$element, FormStateInterface $form_state, $context) {
_advanced_text_formatter_widget_alter($element, $form_state, $context);
}
function _advanced_text_formatter_widget_alter(&$element, FormStateInterface $form_state, $context) {
if (isset($element['#type']) && $element['#type'] == 'text_format') {
_advanced_text_formatter_widget_text_format_alter($element, $form_state, $context);
}
elseif (isset($element['value']) && isset($element['value']['#type'])) {
switch ($element['value']['#type']) {
case 'textfield':
_advanced_text_formatter_widget_textfield_alter($element, $form_state, $context);
break;
case 'textarea':
_advanced_text_formatter_widget_textarea_alter($element, $form_state, $context);
break;
}
}
}
function _advanced_text_formatter_widget_textfield_alter(&$element, FormStateInterface $form_state, $context) {
$widget = $context['widget'];
if (!module_exists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
if (!\Drupal::moduleHandler()->moduleExists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
return;
}
......@@ -25,7 +89,7 @@ function advanced_text_formatter_field_widget_text_textarea_form_alter(&$element
$description = trim($element['#description']);
if (!empty($description)) {
if (substr($description, -1) != '.') {
if (Unicode::substr($description, -1) != '.') {
$description .= '. ';
}
else {
......@@ -39,13 +103,10 @@ function advanced_text_formatter_field_widget_text_textarea_form_alter(&$element
$element['value']['#description'] = $description;
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textfield_form_alter(&$element, &$form_state, $context) {
function _advanced_text_formatter_widget_textarea_alter(&$element, FormStateInterface $form_state, $context) {
$widget = $context['widget'];
if (!module_exists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
if (!\Drupal::moduleHandler()->moduleExists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
return;
}
......@@ -67,13 +128,10 @@ function advanced_text_formatter_field_widget_text_textfield_form_alter(&$elemen
$element['value']['#description'] = $description;
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function advanced_text_formatter_field_widget_text_textarea_with_summary_form_alter(&$element, &$form_state, $context) {
function _advanced_text_formatter_widget_text_format_alter(&$element, FormStateInterface $form_state, $context) {
$widget = $context['widget'];
if (!module_exists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
if (!\Drupal::moduleHandler()->moduleExists('token') || !_advanced_text_formatter_show_token_tree($widget)) {
return;
}
......@@ -88,13 +146,15 @@ function advanced_text_formatter_field_widget_text_textarea_with_summary_form_al
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function advanced_text_formatter_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, $form_state) {
function advanced_text_formatter_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$element = array();
if (module_exists('token')) {
if (\Drupal::moduleHandler()->moduleExists('token')) {
switch ($plugin->getPluginId()) {
case 'text_textarea':
case 'string_textfield':
case 'string_textarea':
case 'text_textfield':
case 'text_textarea':
case 'text_textarea_with_summary':
$element['show_token_tree'] = array(
'#type' => 'checkbox',
......@@ -114,7 +174,7 @@ function advanced_text_formatter_field_widget_third_party_settings_form(WidgetIn
* Implements hook_field_widget_settings_summary_alter().
*/
function advanced_text_formatter_field_widget_settings_summary_alter(&$summary, $context) {
if (!module_exists('token')) {
if (!\Drupal::moduleHandler()->moduleExists('token')) {
return;
}
......@@ -122,8 +182,10 @@ function advanced_text_formatter_field_widget_settings_summary_alter(&$summary,
$widget_type = $widget->getPluginId();
switch ($widget_type) {
case 'text_textarea':
case 'string_textfield':
case 'string_textarea':
case 'text_textfield':
case 'text_textarea':
case 'text_textarea_with_summary':
if (_advanced_text_formatter_show_token_tree($widget)) {
$summary[] = t("Show available tokens in field's description");
......@@ -155,7 +217,7 @@ function advanced_text_formatter_trim_text($text, $options) {
}
if (drupal_strlen($text) > $options['max_length']) {
$text = drupal_substr($text, 0, $options['max_length']);
$text = Unicode::substr($text, 0, $options['max_length']);
if (!empty($options['word_boundary'])) {
$regex = "(.*)\b.+";
......@@ -198,8 +260,8 @@ function advanced_text_formatter_trim_text($text, $options) {
* @return string
* A HTML link
*/
function _advanced_text_formatter_browse_tokens($token_types) {return 'test';
if (!module_exists('token')) {
function _advanced_text_formatter_browse_tokens($token_types) {
if (!\Drupal::moduleHandler()->moduleExists('token')) {
return;
}
......@@ -258,7 +320,7 @@ function _advanced_text_formatter_field_add_token_tree($element) {
* @param array $form_state
* Form state.
*/
function _advanced_text_formatter_validate_allowed_html($element, &$form_state) {
function _advanced_text_formatter_validate_allowed_html($element, FormStateInterface $form_state) {
$tags = array();
$value = isset($element['#value']) ? trim($element['#value']) : '';
......@@ -266,5 +328,5 @@ function _advanced_text_formatter_validate_allowed_html($element, &$form_state)
$tags = preg_split('/\s+|<|>/', $value, -1, PREG_SPLIT_NO_EMPTY);
}
NestedArray::setValue($form_state['values'], $element['#parents'], $tags);
$form_state->setValue($element['#parents'], $tags);
}
......@@ -8,8 +8,8 @@
namespace Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Field\Annotation\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
......@@ -21,6 +21,8 @@ use Drupal\Core\Field\FieldItemListInterface;
* module = "advanced_text_formatter",
* label = @Translation("Advanced Text"),
* field_types = {
* "string",
* "string_long",
* "text",
* "text_long",
* "text_with_summary",
......@@ -31,6 +33,11 @@ use Drupal\Core\Field\FieldItemListInterface;
* )
*/
class AdvancedTextFormatter extends FormatterBase {
const FORMAT_DRUPAL = 'drupal';
const FORMAT_INPUT = 'input';
const FORMAT_NONE = 'none';
const FORMAT_PHP = 'php';
/**
* {@inheritdoc}
*/
......@@ -51,12 +58,12 @@ class AdvancedTextFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$elid_trim = drupal_html_id('advanced_text_formatter_trim');
$elid_filter = drupal_html_id('advanced_text_formatter_filter');
public function settingsForm(array $form, FormStateInterface $form_state) {
$elTrimLengthId = Html::getUniqueId('advanced_text_formatter_trim');
$elFilterId = Html::getUniqueId('advanced_text_formatter_filter');
$element['trim_length'] = array(
'#id' => $elid_trim,
'#id' => $elTrimLengthId,
'#type' => 'number',
'#title' => t('Trim length'),
'#description' => t("Set this to 0 if you don't want to cut the text. Otherwise, input a positive integer."),
......@@ -72,7 +79,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value' => $this->getSetting('ellipsis'),
'#states' => array(
'visible' => array(
'#' . $elid_trim => array('!value' => '0'),
'#' . $elTrimLengthId => array('!value' => '0'),
),
),
);
......@@ -84,7 +91,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value' => $this->getSetting('word_boundary'),
'#states' => array(
'visible' => array(
'#' . $elid_trim => array('!value' => '0'),
'#' . $elTrimLengthId => array('!value' => '0'),
),
),
);
......@@ -109,14 +116,14 @@ class AdvancedTextFormatter extends FormatterBase {
);
$element['filter'] = array(
'#id' => $elid_filter,
'#id' => $elFilterId,
'#title' => t('Filter'),
'#type' => 'select',
'#options' => array(
'none' => t('None'),
'input' => t('Selected Text Format'),
'php' => t('Limit allowed HTML tags'),
'drupal' => t('Drupal'),
static::FORMAT_NONE => t('None'),
static::FORMAT_INPUT => t('Selected Text Format'),
static::FORMAT_PHP => t('Limit allowed HTML tags'),
static::FORMAT_DRUPAL => t('Drupal'),
),
'#default_value' => $this->getSetting('filter'),
);
......@@ -128,27 +135,27 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value' => $this->getSetting('format'),
'#states' => array(
'visible' => array(
'#' . $elid_filter => array('value' => 'drupal'),
'#' . $elFilterId => array('value' => 'drupal'),
),
),
);
$formats = filter_formats();
foreach ($formats as $format_id => $format) {
$element['format']['#options'][$format_id] = $format->name;
foreach ($formats as $formatId => $format) {
$element['format']['#options'][$formatId] = $format->name;
}
$allowed_html = $this->getSetting('allowed_html');
$allowedHtml = $this->getSetting('allowed_html');
if (empty($allowed_html)) {
if (empty($allowedHtml)) {
$tags = '';
}
elseif (is_string($allowed_html)) {
$tags = $allowed_html;
elseif (is_string($allowedHtml)) {
$tags = $allowedHtml;
}
else {
$tags = '<' . implode('> <', $allowed_html) . '>';
$tags = '<' . implode('> <', $allowedHtml) . '>';
}
$element['allowed_html'] = array(
......@@ -161,7 +168,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#element_validate' => array('_advanced_text_formatter_validate_allowed_html'),
'#states' => array(
'visible' => array(
'#' . $elid_filter => array('value' => 'php'),
'#' . $elFilterId => array('value' => 'php'),
),
),
);
......@@ -173,7 +180,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value' => $this->getSetting('autop'),
'#states' => array(
'invisible' => array(
'#' . $elid_filter => array('!value' => 'php'),
'#' . $elFilterId => array('!value' => 'php'),
),
),
);
......@@ -202,7 +209,7 @@ class AdvancedTextFormatter extends FormatterBase {
$summary[] = t('Token Replace') . ': ' . ($this->getSetting('token_replace') ? ($yes . '. ' . $token_link) : $no);
switch ($this->getSetting('filter')) {
case 'drupal':
case static::FORMAT_DRUPAL:
$formats = filter_formats();
$format = $this->getSetting('format');
$format = isset($formats[$format]) ? $formats[$format]->name : t('Unknown');
......@@ -212,7 +219,7 @@ class AdvancedTextFormatter extends FormatterBase {
break;
case 'php':
case static::FORMAT_PHP:
$text = array();
$tags = $this->getSetting('allowed_html');
$autop = $this->getSetting('autop');
......@@ -236,7 +243,7 @@ class AdvancedTextFormatter extends FormatterBase {
break;
case 'input':
case static::FORMAT_INPUT:
$summary[] = t('Filter: @filter', array('@filter' => t('Selected Text Format')));
break;
......@@ -275,12 +282,12 @@ class AdvancedTextFormatter extends FormatterBase {
}
switch ($this->getSetting('filter')) {
case 'drupal':
case static::FORMAT_DRUPAL:
$output = check_markup($output, $this->getSetting('format'), $item->getLangcode());
break;
case 'php':
case static::FORMAT_PHP:
$output = Xss::filter($output, $this->getSetting('allowed_html'));
if ($this->getSetting('autop')) {
......@@ -289,7 +296,7 @@ class AdvancedTextFormatter extends FormatterBase {
break;
case 'input':
case static::FORMAT_INPUT:
$output = check_markup($output, $item->format, $item->getLangcode());
break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment