Advertisement
Guest User

Untitled

a guest
May 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php
  2. index da1c9d0..2339f20 100644
  3. --- a/src/Form/EntityEmbedDialog.php
  4. +++ b/src/Form/EntityEmbedDialog.php
  5. @@ -171,7 +171,7 @@ class EntityEmbedDialog extends FormBase {
  6. $form_state->set('entity', current($entity) ?: NULL);
  7.  
  8. if (!$form_state->get('step')) {
  9. - // If an entity has been selected, then always skip to the embed options.
  10. + // If an entity has been selected, then skip to the embed or edit options.
  11. if ($form_state->get('entity')) {
  12. $form_state->set('step', 'embed');
  13. }
  14. @@ -198,6 +198,9 @@ class EntityEmbedDialog extends FormBase {
  15. elseif ($form_state->get('step') == 'embed') {
  16. $form = $this->buildEmbedStep($form, $form_state);
  17. }
  18. + elseif ($form_state->get('step') == 'entity_edit') {
  19. + $form = $this->buildEntityEditStep($form, $form_state);
  20. + }
  21.  
  22. return $form;
  23. }
  24. @@ -389,6 +392,21 @@ class EntityEmbedDialog extends FormBase {
  25. '#title' => $this->t('Selected entity'),
  26. '#markup' => $entity_label,
  27. ];
  28. +
  29. + $inline_entity_edit_enabled = $embed_button->getTypeSetting('inline_entity_edit_enabled');
  30. + if ($inline_entity_edit_enabled && $this->moduleHandler->moduleExists('inline_entity_form')) {
  31. + // Provide a link to switch to the entity edit step.
  32. + $form['entity_edit'] = [
  33. + '#type' => 'button',
  34. + '#executes_submit_callback' => FALSE,
  35. + '#value' => $this->t('Edit'),
  36. + '#ajax' => [
  37. + 'callback' => '::submitAndShowEntityEdit',
  38. + 'event' => 'click',
  39. + ],
  40. + ];
  41. + }
  42. +
  43. $form['attributes']['data-entity-type'] = [
  44. '#type' => 'hidden',
  45. '#value' => $entity_element['data-entity-type'],
  46. @@ -506,6 +524,121 @@ class EntityEmbedDialog extends FormBase {
  47. return $form;
  48. }
  49.  
  50. + /**
  51. + * Form constructor for the entity embedding step.
  52. + *
  53. + * @param array $form
  54. + * An associative array containing the structure of the form.
  55. + * @param \Drupal\Core\Form\FormStateInterface $form_state
  56. + * The current state of the form.
  57. + *
  58. + * @return array
  59. + * The form structure.
  60. + */
  61. + public function buildEntityEditStep(array $form, FormStateInterface $form_state) {
  62. + // Entity element is calculated on every AJAX request/submit.
  63. + // See ::buildForm().
  64. + $entity_element = $form_state->get('entity_element');
  65. +
  66. + /** @var \Drupal\Core\Entity\EntityInterface $entity */
  67. + $entity = $form_state->get('entity');
  68. +
  69. + $form['#title'] = $this->t('Edit @type', array('@type' => $entity->getEntityType()->getLowercaseLabel()));
  70. +
  71. + if ($this->moduleHandler->moduleExists('inline_entity_form')) {
  72. + // @TODO: Make form_mode configurable?
  73. + $form['inline_entity_form'] = [
  74. + '#type' => 'inline_entity_form',
  75. + '#entity_type' => $entity->getEntityType()->id(),
  76. + '#bundle' => $entity->bundle(),
  77. + '#language' => $entity->language()->getId(),
  78. + '#default_value' => $entity,
  79. + '#op' => 'edit',
  80. + '#form_mode' => 'default',
  81. + '#save_entity' => TRUE,
  82. + ];
  83. +
  84. + $form['attributes']['data-entity-type'] = array(
  85. + '#type' => 'hidden',
  86. + '#value' => $entity_element['data-entity-type'],
  87. + );
  88. + $form['attributes']['data-entity-uuid'] = array(
  89. + '#type' => 'hidden',
  90. + '#value' => $entity_element['data-entity-uuid'],
  91. + );
  92. +
  93. + // TODO: Also set data-entity-embed-display-settings.
  94. + $form['attributes']['data-entity-embed-display'] = array(
  95. + '#type' => 'hidden',
  96. + '#default_value' => $entity_element['data-entity-embed-display'],
  97. + '#required' => TRUE,
  98. + );
  99. + $form['attributes']['data-align'] = array(
  100. + '#type' => 'hidden',
  101. + '#default_value' => isset($entity_element['data-align']) ? $entity_element['data-align'] : '',
  102. + );
  103. + $form['attributes']['data-caption'] = array(
  104. + '#type' => 'hidden',
  105. + '#default_value' => isset($entity_element['data-caption']) ? Html::decodeEntities($entity_element['data-caption']) : '',
  106. + '#element_validate' => array('::escapeValue'),
  107. + );
  108. +
  109. + $form['actions'] = array(
  110. + '#type' => 'actions',
  111. + );
  112. + $form['actions']['back'] = array(
  113. + '#type' => 'submit',
  114. + '#value' => $this->t('Back'),
  115. + // No regular submit-handler. This form only works via JavaScript.
  116. + '#submit' => array(),
  117. + '#ajax' => array(
  118. + 'callback' => '::submitAndShowEmbed',
  119. + 'event' => 'click',
  120. + ),
  121. + );
  122. + $form['actions']['save_modal'] = array(
  123. + '#type' => 'submit',
  124. + '#value' => $this->t('Save'),
  125. + '#button_type' => 'primary',
  126. + // Pretend to be IEFs submit button.
  127. + '#submit' => array(
  128. + [
  129. + 'Drupal\inline_entity_form\ElementSubmit',
  130. + 'trigger'
  131. + ]
  132. + ),
  133. + '#ief_submit_trigger' => TRUE,
  134. + '#ief_submit_trigger_all' => TRUE,
  135. + // No regular submit-handler. This form only works via JavaScript.
  136. + '#ajax' => array(
  137. + 'callback' => '::submitEntityEditStep',
  138. + 'event' => 'click',
  139. + ),
  140. + );
  141. + }
  142. + else {
  143. + // Without the inline entity form module, the only option is to go back.
  144. + $form['message'] = array(
  145. + '#markup' => $this->t('The Inline Entity Form module is required.'),
  146. + );
  147. + $form['actions'] = array(
  148. + '#type' => 'actions',
  149. + );
  150. + $form['actions']['back'] = array(
  151. + '#type' => 'submit',
  152. + '#value' => $this->t('Back'),
  153. + // No regular submit-handler. This form only works via JavaScript.
  154. + '#submit' => array(),
  155. + '#ajax' => array(
  156. + 'callback' => '::submitAndShowEmbed',
  157. + 'event' => 'click',
  158. + ),
  159. + );
  160. + }
  161. +
  162. + return $form;
  163. + }
  164. +
  165. /**
  166. * {@inheritdoc}
  167. */
  168. @@ -722,6 +855,21 @@ class EntityEmbedDialog extends FormBase {
  169. return $this->submitStep($form, $form_state, 'embed');
  170. }
  171.  
  172. + /**
  173. + * Submit and show entity edit step after submit.
  174. + *
  175. + * @param array $form
  176. + * The form array.
  177. + * @param \Drupal\Core\Form\FormStateInterface $form_state
  178. + * The form state.
  179. + *
  180. + * @return \Drupal\Core\Ajax\AjaxResponse
  181. + * The ajax response.
  182. + */
  183. + public function submitAndShowEntityEdit(array $form, FormStateInterface $form_state) {
  184. + return $this->submitStep($form, $form_state, 'entity_edit');
  185. + }
  186. +
  187. /**
  188. * Form submission handler for the entity embedding step.
  189. *
  190. @@ -785,6 +933,46 @@ class EntityEmbedDialog extends FormBase {
  191. return $response;
  192. }
  193.  
  194. + /**
  195. + * Form submission handler for the entity editing step.
  196. + *
  197. + * On success this will submit the command to save the embedded entity with
  198. + * the configured display settings to the WYSIWYG element, and then close the
  199. + * modal dialog. On form errors, this will rebuild the form and display the
  200. + * error messages.
  201. + *
  202. + * @param array $form
  203. + * An associative array containing the structure of the form.
  204. + * @param FormStateInterface $form_state
  205. + * An associative array containing the current state of the form.
  206. + *
  207. + * @return \Drupal\Core\Ajax\AjaxResponse
  208. + * The ajax response.
  209. + */
  210. + public function submitEntityEditStep(array &$form, FormStateInterface $form_state) {
  211. + $response = new AjaxResponse();
  212. +
  213. + // Display errors in form, if any.
  214. + if ($form_state->hasAnyErrors()) {
  215. + unset($form['#prefix'], $form['#suffix']);
  216. + $form['status_messages'] = array(
  217. + '#type' => 'status_messages',
  218. + '#weight' => -10,
  219. + );
  220. + $response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $form));
  221. + }
  222. + else {
  223. + $form_state->set('step', 'embed');
  224. + $form_state->setRebuild(TRUE);
  225. + $rebuild_form = $this->formBuilder->rebuildForm('entity_embed_dialog', $form_state, $form);
  226. + unset($rebuild_form['#prefix'], $rebuild_form['#suffix']);
  227. + $response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $rebuild_form));
  228. + $response->addCommand(new SetDialogTitleCommand('', $rebuild_form['#title']));
  229. + }
  230. +
  231. + return $response;
  232. + }
  233. +
  234. /**
  235. * Form element validation handler; Escapes the value an element.
  236. *
  237. diff --git a/src/Plugin/EmbedType/Entity.php b/src/Plugin/EmbedType/Entity.php
  238. index 42bb3fa..55ab741 100644
  239. --- a/src/Plugin/EmbedType/Entity.php
  240. +++ b/src/Plugin/EmbedType/Entity.php
  241. @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  242. use Drupal\Core\Entity\EntityTypeInterface;
  243. use Drupal\Core\Entity\EntityTypeManagerInterface;
  244. use Drupal\Core\Entity\EntityTypeRepositoryInterface;
  245. +use Drupal\Core\Extension\ModuleHandlerInterface;
  246. use Drupal\Core\Form\FormStateInterface;
  247. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  248. use Drupal\Core\Plugin\PluginDependencyTrait;
  249. @@ -53,6 +54,13 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  250. */
  251. protected $displayPluginManager;
  252.  
  253. + /**
  254. + * The module handler.
  255. + *
  256. + * @var \Drupal\Core\Extension\ModuleHandlerInterface
  257. + */
  258. + protected $moduleHandler;
  259. +
  260. /**
  261. * {@inheritdoc}
  262. *
  263. @@ -70,13 +78,16 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  264. * The entity type bundle info service.
  265. * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager
  266. * The plugin manager.
  267. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  268. + * The module handler.
  269. */
  270. - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleInfoInterface $bundle_info, EntityEmbedDisplayManager $display_plugin_manager) {
  271. + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleInfoInterface $bundle_info, EntityEmbedDisplayManager $display_plugin_manager, ModuleHandlerInterface $module_handler) {
  272. parent::__construct($configuration, $plugin_id, $plugin_definition);
  273. $this->entityTypeManager = $entity_type_manager;
  274. $this->entityTypeRepository = $entity_type_repository;
  275. $this->entityTypeBundleInfo = $bundle_info;
  276. $this->displayPluginManager = $display_plugin_manager;
  277. + $this->moduleHandler = $module_handler;
  278. }
  279.  
  280. /**
  281. @@ -90,7 +101,8 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  282. $container->get('entity_type.manager'),
  283. $container->get('entity_type.repository'),
  284. $container->get('entity_type.bundle.info'),
  285. - $container->get('plugin.manager.entity_embed.display')
  286. + $container->get('plugin.manager.entity_embed.display'),
  287. + $container->get('module_handler')
  288. );
  289. }
  290.  
  291. @@ -106,6 +118,7 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  292. 'entity_browser_settings' => [
  293. 'display_review' => 0,
  294. ],
  295. + 'inline_entity_edit_enabled' => FALSE,
  296. ];
  297. }
  298.  
  299. @@ -196,6 +209,15 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  300. }
  301. }
  302.  
  303. + if ($this->moduleHandler->moduleExists('inline_entity_form')) {
  304. + $form['inline_entity_edit_enabled'] = [
  305. + '#type' => 'checkbox',
  306. + '#title' => $this->t('Enable inline editing'),
  307. + '#description' => $this->t('Allow embedded entities to be editable within the embed dialog.'),
  308. + '#default_value' => $this->getConfigurationValue('inline_entity_edit_enabled'),
  309. + ];
  310. + }
  311. +
  312. return $form;
  313. }
  314.  
  315. @@ -211,6 +233,9 @@ class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  316. $entity_browser = $form_state->getValue('entity_browser') == '_none' ? '' : $form_state->getValue('entity_browser');
  317. $form_state->setValue('entity_browser', $entity_browser);
  318. $form_state->setValue('entity_browser_settings', $form_state->getValue('entity_browser_settings'));
  319. + if ($form_state->hasValue('inline_entity_edit_enabled')) {
  320. + $form_state->setValue('inline_entity_edit_enabled', $form_state->getValue('inline_entity_edit_enabled'));
  321. + }
  322.  
  323. parent::submitConfigurationForm($form, $form_state);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement