Skip to content

Instantly share code, notes, and snippets.

@badri
Created May 21, 2019 00:23
Show Gist options
  • Save badri/74ad393f4116bf4066bbf6fe4733e77e to your computer and use it in GitHub Desktop.
Save badri/74ad393f4116bf4066bbf6fe4733e77e to your computer and use it in GitHub Desktop.
Patch
diff --git a/.gitignore b/.gitignore
index f2fb0bf..854dfd6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,4 @@ Thumbs.db
/glazed_builder/glazed_frontend.min.js
/glazed_builder/glazed_param_types.js
/glazed_builder/glazed_param_types.min.js
+*~
diff --git a/src/Controller/AjaxController.php b/src/Controller/AjaxController.php
index 1163703..bc75d45 100644
--- a/src/Controller/AjaxController.php
+++ b/src/Controller/AjaxController.php
@@ -570,7 +570,12 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
*/
private function saveContainer($entityType, $bundle, $entityId, $fieldName, $encodedHtml, $langcode) {
// Saves Glazed Builder container instance to field, respecting permissions, language and revisions if supported
- $entity = $this->entityTypeManager->getStorage($entityType)->load($entityId);
+ if ($this->entityTypeManager->getDefinition($entityType)->getKey('revision')) {
+ $entity = $this->entityTypeManager->getStorage($entityType)->loadRevision($entityId);
+ }
+ else {
+ $entity = $this->entityTypeManager->getStorage($entityType)->load($entityId);
+ }
if ($entity && $entity->access('update', $this->currentUser)) {
if ($entity->isTranslatable()) {
$languages = $entity->getTranslationLanguages();
@@ -595,8 +600,13 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
$entity->setRevisionUserId($this->currentUser->id());
$entity->setRevisionCreationTime($this->getRequestTime());
}
+ $current_state = $entity->get('moderation_state')->getString();
+ if ($current_state == 'published') {
+ $entity->set('moderation_state', 'published');
+ } else {
+ $entity->set('moderation_state', 'draft');
+ }
}
-
$entity->save();
return new JsonResponse('');
diff --git a/src/Plugin/Field/FieldFormatter/GlazedBuilderFormatter.php b/src/Plugin/Field/FieldFormatter/GlazedBuilderFormatter.php
index a2dc8e8..08dc18e 100644
--- a/src/Plugin/Field/FieldFormatter/GlazedBuilderFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/GlazedBuilderFormatter.php
@@ -8,6 +8,7 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Entity;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
@@ -103,6 +104,13 @@ class GlazedBuilderFormatter extends FormatterBase implements ContainerFactoryPl
*/
protected $glazedBuilderService;
+ /**
+ * The entity type manager service
+ *
+ * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+ */
+ protected $entityTypeManager;
+
/**
* Construct a GlazedBuilderFormatter object
*
@@ -138,8 +146,10 @@ class GlazedBuilderFormatter extends FormatterBase implements ContainerFactoryPl
* The renderer service
* @param \Drupal\glazed_builder\Service\GlazedBuilderServiceInterface $glazedBuilderService
* The glazed builder service
+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
+ * The entity type manager service
*/
- public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, CurrentPathStack $currentPathStack, RequestStack $requestStack, LanguageManagerInterface $languageManager, CsrfTokenGenerator $csrfToken, ModuleHandlerInterface $moduleHandler, RendererInterface $renderer, GlazedBuilderServiceInterface $glazedBuilderService) {
+ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, CurrentPathStack $currentPathStack, RequestStack $requestStack, LanguageManagerInterface $languageManager, CsrfTokenGenerator $csrfToken, ModuleHandlerInterface $moduleHandler, RendererInterface $renderer, GlazedBuilderServiceInterface $glazedBuilderService, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $currentUser;
@@ -151,6 +161,7 @@ class GlazedBuilderFormatter extends FormatterBase implements ContainerFactoryPl
$this->moduleHandler = $moduleHandler;
$this->renderer = $renderer;
$this->glazedBuilderService = $glazedBuilderService;
+ $this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
@@ -170,7 +181,8 @@ class GlazedBuilderFormatter extends FormatterBase implements ContainerFactoryPl
$container->get('csrf_token'),
$container->get('module_handler'),
$container->get('renderer'),
- $container->get('glazed_builder.service')
+ $container->get('glazed_builder.service'),
+ $container->get('entity_type.manager')
);
}
@@ -193,7 +205,12 @@ class GlazedBuilderFormatter extends FormatterBase implements ContainerFactoryPl
$entity_type = $this->fieldDefinition->get('entity_type');
$bundle = $this->fieldDefinition->get('bundle');
$entity = $items->getEntity();
- $id = $entity->id();
+ if ($this->entityTypeManager->getDefinition($entity_type)->getKey('revision')) {
+ $id = $entity->getRevisionId();
+ }
+ else {
+ $id = $entity->id();
+ }
$field_name = $this->fieldDefinition->get('field_name');
$entity_label = $entity->label();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment