forked from kalamuna/autogrow_textarea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautogrow_textarea.module
69 lines (64 loc) · 2.82 KB
/
autogrow_textarea.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function autogrow_textarea_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$element = [];
if (in_array($plugin->getPluginId(), ['text_textarea', 'text_textarea_with_summary'])) {
$element['autogrow_textarea'] = array(
'#type' => 'details',
'#title' => t('Autogrow'),
'#open' => TRUE,
'#tree' => FALSE,
);
$element['autogrow_textarea_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable autogrow'),
'#default_value' => $plugin->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_enabled'),
'#group' => 'autogrow_textarea',
);
$element['autogrow_textarea_min_height'] = array(
'#type' => 'textfield',
'#title' => t('Min height'),
'#description' => t('The css min-height value the textarea may shrink to.'),
'#default_value' => $plugin->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_min_height'),
'#group' => 'autogrow_textarea',
);
$element['autogrow_textarea_max_height'] = array(
'#type' => 'textfield',
'#title' => t('Max height'),
'#description' => t('The css max-height value the textarea may grow to.'),
'#default_value' => $plugin->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_max_height'),
'#group' => 'autogrow_textarea',
);
}
return $element;
}
/**
* Implements hook_field_widget_settings_summary_alter().
*/
function autogrow_textarea_field_widget_settings_summary_alter(&$summary, $context) {
if (in_array($context['widget']->getPluginId(), ['text_textarea', 'text_textarea_with_summary'])) {
if ($context['widget']->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_enabled')) {
$summary[] = t('Autogrow enabled');
}
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function autogrow_textarea_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
if ($context['widget']->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_enabled', FALSE)) {
$element['#attributes']['class'][] = 'autogrow-textarea';
$element['#attached']['library'][] = 'autogrow_textarea/autogrow_textarea_behaviors';
if($max_height = $context['widget']->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_max_height')){
$element['#attributes']['data-autogrow-max'] = $max_height;
}
if ($min_height = $context['widget']->getThirdPartySetting('autogrow_textarea', 'autogrow_textarea_min_height')) {
$element['#attributes']['data-autogrow-min'] = $min_height;
}
}
}