Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
advanced_text_formatter
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
advanced_text_formatter
Commits
032be677
Commit
032be677
authored
Sep 28, 2014
by
Nhat Tran
Browse files
Options
Downloads
Patches
Plain Diff
Adapt to Drupal changes
parent
ea3fdcb1
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
advanced_text_formatter.module
+86
-24
86 additions, 24 deletions
advanced_text_formatter.module
src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php
+36
-29
36 additions, 29 deletions
src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php
with
122 additions
and
53 deletions
advanced_text_formatter.module
+
86
−
24
View file @
032be677
...
...
@@ -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_e
xists
(
'token'
)
||
!
_advanced_text_formatter_show_token_tree
(
$widget
))
{
if
(
!
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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_e
xists
(
'token'
)
||
!
_advanced_text_formatter_show_token_tree
(
$widget
))
{
if
(
!
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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_e
xists
(
'token'
)
||
!
_advanced_text_formatter_show_token_tree
(
$widget
))
{
if
(
!
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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_e
xists
(
'token'
))
{
if
(
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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_e
xists
(
'token'
))
{
if
(
!
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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_e
xists
(
'token'
))
{
function
_advanced_text_formatter_browse_tokens
(
$token_types
)
{
if
(
!
\Drupal
::
moduleHandler
()
->
moduleE
xists
(
'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
);
}
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php
+
36
−
29
View file @
032be677
...
...
@@ -8,8 +8,8 @@
namespace
Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter
;
use
Drupal\Component\Utility\Xss
;
use
Drupal\Co
re\Annotation\Translation
;
use
Drupal\Core\F
ield\Annotation\FieldFormatter
;
use
Drupal\Co
mponent\Utility\Html
;
use
Drupal\Core\F
orm\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
)
{
$el
id_trim
=
drupal_html_i
d
(
'advanced_text_formatter_trim'
);
$el
id_f
ilter
=
drupal_html_i
d
(
'advanced_text_formatter_filter'
);
public
function
settingsForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$el
TrimLengthId
=
Html
::
getUniqueI
d
(
'advanced_text_formatter_trim'
);
$el
F
ilter
Id
=
Html
::
getUniqueI
d
(
'advanced_text_formatter_filter'
);
$element
[
'trim_length'
]
=
array
(
'#id'
=>
$el
id_trim
,
'#id'
=>
$el
TrimLengthId
,
'#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
(
'#'
.
$el
id_trim
=>
array
(
'!value'
=>
'0'
),
'#'
.
$el
TrimLengthId
=>
array
(
'!value'
=>
'0'
),
),
),
);
...
...
@@ -84,7 +91,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value'
=>
$this
->
getSetting
(
'word_boundary'
),
'#states'
=>
array
(
'visible'
=>
array
(
'#'
.
$el
id_trim
=>
array
(
'!value'
=>
'0'
),
'#'
.
$el
TrimLengthId
=>
array
(
'!value'
=>
'0'
),
),
),
);
...
...
@@ -109,14 +116,14 @@ class AdvancedTextFormatter extends FormatterBase {
);
$element
[
'filter'
]
=
array
(
'#id'
=>
$el
id_f
ilter
,
'#id'
=>
$el
F
ilter
Id
,
'#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
(
'#'
.
$el
id_f
ilter
=>
array
(
'value'
=>
'drupal'
),
'#'
.
$el
F
ilter
Id
=>
array
(
'value'
=>
'drupal'
),
),
),
);
$formats
=
filter_formats
();
foreach
(
$formats
as
$format
_i
d
=>
$format
)
{
$element
[
'format'
][
'#options'
][
$format
_i
d
]
=
$format
->
name
;
foreach
(
$formats
as
$format
I
d
=>
$format
)
{
$element
[
'format'
][
'#options'
][
$format
I
d
]
=
$format
->
name
;
}
$allowed
_h
tml
=
$this
->
getSetting
(
'allowed_html'
);
$allowed
H
tml
=
$this
->
getSetting
(
'allowed_html'
);
if
(
empty
(
$allowed
_h
tml
))
{
if
(
empty
(
$allowed
H
tml
))
{
$tags
=
''
;
}
elseif
(
is_string
(
$allowed
_h
tml
))
{
$tags
=
$allowed
_h
tml
;
elseif
(
is_string
(
$allowed
H
tml
))
{
$tags
=
$allowed
H
tml
;
}
else
{
$tags
=
'<'
.
implode
(
'> <'
,
$allowed
_h
tml
)
.
'>'
;
$tags
=
'<'
.
implode
(
'> <'
,
$allowed
H
tml
)
.
'>'
;
}
$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
(
'#'
.
$el
id_f
ilter
=>
array
(
'value'
=>
'php'
),
'#'
.
$el
F
ilter
Id
=>
array
(
'value'
=>
'php'
),
),
),
);
...
...
@@ -173,7 +180,7 @@ class AdvancedTextFormatter extends FormatterBase {
'#default_value'
=>
$this
->
getSetting
(
'autop'
),
'#states'
=>
array
(
'invisible'
=>
array
(
'#'
.
$el
id_f
ilter
=>
array
(
'!value'
=>
'php'
),
'#'
.
$el
F
ilter
Id
=>
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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment