|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Contains \Drupal\url_embed\Form\UrlEmbedDialog. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Drupal\url_embed\Form; |
|
9
|
|
|
|
|
10
|
|
|
use Drupal\Core\Ajax\AjaxResponse; |
|
11
|
|
|
use Drupal\Core\Ajax\CloseModalDialogCommand; |
|
12
|
|
|
use Drupal\Core\Ajax\HtmlCommand; |
|
13
|
|
|
use Drupal\Core\Form\FormBase; |
|
14
|
|
|
use Drupal\Core\Form\FormBuilderInterface; |
|
15
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
16
|
|
|
use Drupal\editor\Ajax\EditorDialogSave; |
|
17
|
|
|
use Drupal\editor\EditorInterface; |
|
18
|
|
|
use Drupal\embed\EmbedButtonInterface; |
|
19
|
|
|
use Drupal\url_embed\UrlEmbedHelperTrait; |
|
20
|
|
|
use Drupal\url_embed\UrlEmbedInterface; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Provides a form to embed URLs. |
|
25
|
|
|
*/ |
|
26
|
|
|
class UrlEmbedDialog extends FormBase { |
|
27
|
|
|
use UrlEmbedHelperTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The form builder. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Drupal\Core\Form\FormBuilderInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $formBuilder; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructs a UrlEmbedDialog object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Drupal\url_embed\UrlEmbedInterface $url_embed |
|
40
|
|
|
* The URL embed service. |
|
41
|
|
|
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
|
42
|
|
|
* The Form Builder. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(UrlEmbedInterface $url_embed, FormBuilderInterface $form_builder) { |
|
45
|
|
|
$this->setUrlEmbed($url_embed); |
|
46
|
|
|
$this->formBuilder = $form_builder; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function create(ContainerInterface $container) { |
|
53
|
|
|
return new static( |
|
54
|
|
|
$container->get('url_embed'), |
|
55
|
|
|
$container->get('form_builder') |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getFormId() { |
|
63
|
|
|
return 'url_embed_dialog'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Drupal\editor\EditorInterface $editor |
|
70
|
|
|
* The editor to which this dialog corresponds. |
|
71
|
|
|
* @param \Drupal\embed\EmbedButtonInterface $embed_button |
|
72
|
|
|
* The URL button to which this dialog corresponds. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) { |
|
75
|
|
|
$values = $form_state->getValues(); |
|
76
|
|
|
$input = $form_state->getUserInput(); |
|
77
|
|
|
// Set URL button element in form state, so that it can be used later in |
|
78
|
|
|
// validateForm() function. |
|
79
|
|
|
$form_state->set('embed_button', $embed_button); |
|
80
|
|
|
$form_state->set('editor', $editor); |
|
81
|
|
|
// Initialize URL element with form attributes, if present. |
|
82
|
|
|
$url_element = empty($values['attributes']) ? array() : $values['attributes']; |
|
83
|
|
|
$url_element += empty($input['attributes']) ? array() : $input['attributes']; |
|
84
|
|
|
// The default values are set directly from \Drupal::request()->request, |
|
85
|
|
|
// provided by the editor plugin opening the dialog. |
|
86
|
|
|
if (!$form_state->get('url_element')) { |
|
87
|
|
|
$form_state->set('url_element', isset($input['editor_object']) ? $input['editor_object'] : array()); |
|
88
|
|
|
} |
|
89
|
|
|
$url_element += $form_state->get('url_element'); |
|
90
|
|
|
$url_element += array( |
|
91
|
|
|
'data-embed-url' => '', |
|
92
|
|
|
'data-url-provider' => '', |
|
93
|
|
|
); |
|
94
|
|
|
$form_state->set('url_element', $url_element); |
|
95
|
|
|
|
|
96
|
|
|
$form['#tree'] = TRUE; |
|
97
|
|
|
$form['#attached']['library'][] = 'editor/drupal.editor.dialog'; |
|
98
|
|
|
$form['#prefix'] = '<div id="url-embed-dialog-form">'; |
|
99
|
|
|
$form['#suffix'] = '</div>'; |
|
100
|
|
|
|
|
101
|
|
|
$form['attributes']['data-embed-url'] = array( |
|
102
|
|
|
'#type' => 'textfield', |
|
103
|
|
|
'#title' => 'URL', |
|
104
|
|
|
'#default_value' => $url_element['data-embed-url'], |
|
105
|
|
|
'#required' => TRUE, |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
if (!empty($url_element['data-embed-url']) && $info = $this->urlEmbed()->getEmbed($url_element['data-embed-url'])) { |
|
110
|
|
|
$url_element['data-url-provider'] = $info->getProviderName(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
catch (\Exception $e) { |
|
114
|
|
|
watchdog_exception('url_embed', $e); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$form['attributes']['data-url-provider'] = array( |
|
118
|
|
|
'#type' => 'value', |
|
119
|
|
|
'#value' => $url_element['data-url-provider'], |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$form['attributes']['data-embed-button'] = array( |
|
123
|
|
|
'#type' => 'value', |
|
124
|
|
|
'#value' => $embed_button->id(), |
|
125
|
|
|
); |
|
126
|
|
|
$form['attributes']['data-entity-label'] = array( |
|
127
|
|
|
'#type' => 'value', |
|
128
|
|
|
'#value' => $embed_button->label(), |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$form['actions'] = array( |
|
132
|
|
|
'#type' => 'actions', |
|
133
|
|
|
); |
|
134
|
|
|
$form['actions']['save_modal'] = array( |
|
135
|
|
|
'#type' => 'submit', |
|
136
|
|
|
'#value' => $this->t('Embed'), |
|
137
|
|
|
'#button_type' => 'primary', |
|
138
|
|
|
// No regular submit-handler. This form only works via JavaScript. |
|
139
|
|
|
'#submit' => array(), |
|
140
|
|
|
'#ajax' => array( |
|
141
|
|
|
'callback' => '::submitForm', |
|
142
|
|
|
'event' => 'click', |
|
143
|
|
|
), |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
return $form; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
|
153
|
|
|
$response = new AjaxResponse(); |
|
154
|
|
|
|
|
155
|
|
|
$values = $form_state->getValues(); |
|
156
|
|
|
// Display errors in form, if any. |
|
157
|
|
|
if ($form_state->hasAnyErrors()) { |
|
158
|
|
|
unset($form['#prefix'], $form['#suffix']); |
|
159
|
|
|
$form['status_messages'] = array( |
|
160
|
|
|
'#type' => 'status_messages', |
|
161
|
|
|
'#weight' => -10, |
|
162
|
|
|
); |
|
163
|
|
|
$response->addCommand(new HtmlCommand('#url-embed-dialog-form', $form)); |
|
164
|
|
|
} |
|
165
|
|
|
else { |
|
166
|
|
|
$response->addCommand(new EditorDialogSave($values)); |
|
167
|
|
|
$response->addCommand(new CloseModalDialogCommand()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $response; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|