Skip to content
Permalink
Browse files

Object Preview doesn't work with relations (#5578)

* Object Preview doesn't work with relations #5106

* - moved methods from trait do parent class
- made removeObjectFromSession() available again + added bc layer + marked as deprecated
- marked getObjectFromSession() as deprecated and replaced it with the successor

* - added PhpDoc types
- removed pimcore_documents session attribute bag -> not used anymore

* phpdoc

Co-authored-by: Bernhard Rusch <brusch@users.noreply.github.com>
  • Loading branch information
weisswurstkanone and brusch committed Jan 15, 2020
1 parent 630d01e commit abadacefc316e01247cd4683c54a8ac84b2b0815
@@ -535,7 +535,7 @@ public function getAction(Request $request, EventDispatcherInterface $eventDispa
$eventDispatcher->dispatch(AdminEvents::OBJECT_GET_PRE_SEND_DATA, $event);
$data = $event->getArgument('data');

DataObject\Service::removeObjectFromSession($object->getId());
DataObject\Service::removeElementFromSession('object', $object->getId());

return $this->adminJson($data);
}
@@ -1328,11 +1328,7 @@ public function saveAction(Request $request)
'treeData' => $treeData
]);
} elseif ($request->get('task') == 'session') {
Tool\Session::useSession(function (AttributeBagInterface $session) use ($object) {
$key = 'object_' . $object->getId();
$session->set($key, $object);
}, 'pimcore_objects');

DataObject\Service::saveElementToSession($object);
return $this->adminJson(['success' => true]);
} elseif ($request->get('task') == 'scheduler') {
if ($object->isAllowed('settings')) {
@@ -2046,45 +2042,38 @@ public function copyAction(Request $request)
public function previewAction(Request $request)
{
$id = $request->get('id');
$key = 'object_' . $id;

$session = Tool\Session::getReadOnly('pimcore_objects');
if ($session->has($key)) {
/**
* @var DataObject\Concrete $object
*/
$object = $session->get($key);
} else {
return new Response("Preview not available, it seems that there's a problem with this object.");
}

$url = $object->getClass()->getPreviewUrl();
if ($url) {
// replace named variables
$vars = $object->getObjectVars();
foreach ($vars as $key => $value) {
if (!empty($value) && (is_string($value) || is_numeric($value))) {
$url = str_replace('%' . $key, urlencode($value), $url);
} else {
if (strpos($url, '%' . $key) !== false) {
return new Response('No preview available, please ensure that all fields which are required for the preview are filled correctly.');
if ($object = DataObject\Service::getElementFromSession('object', $id)) {
$url = $object->getClass()->getPreviewUrl();
if ($url) {
// replace named variables
$vars = $object->getObjectVars();
foreach ($vars as $key => $value) {
if (!empty($value) && (is_string($value) || is_numeric($value))) {
$url = str_replace('%' . $key, urlencode($value), $url);
} else {
if (strpos($url, '%' . $key) !== false) {
return new Response('No preview available, please ensure that all fields which are required for the preview are filled correctly.');
}
}
}
} elseif ($linkGenerator = $object->getClass()->getLinkGenerator()) {
$url = $linkGenerator->generate($object, ['preview' => true, 'context' => $this]);
}
} elseif ($linkGenerator = $object->getClass()->getLinkGenerator()) {
$url = $linkGenerator->generate($object, ['preview' => true, 'context' => $this]);
}

if (!$url) {
return new Response("Preview not available, it seems that there's a problem with this object.");
}
if (!$url) {
return new Response("Preview not available, it seems that there's a problem with this object.");
}

// replace all remainaing % signs
$url = str_replace('%', '%25', $url);
// replace all remainaing % signs
$url = str_replace('%', '%25', $url);

$urlParts = parse_url($url);
$urlParts = parse_url($url);

return $this->redirect($urlParts['path'] . '?pimcore_object_preview=' . $id . '&_dc=' . time() . (isset($urlParts['query']) ? '&' . $urlParts['query'] : ''));
return $this->redirect($urlParts['path'] . '?pimcore_object_preview=' . $id . '&_dc=' . time() . (isset($urlParts['query']) ? '&' . $urlParts['query'] : ''));
} else {
return new Response("Preview not available, it seems that there's a problem with this object.");
}
}

/**
@@ -637,12 +637,7 @@ public function versionToSessionAction(Request $request)
{
$version = Version::getById($request->get('id'));
$document = $version->loadData();

Session::useSession(function (AttributeBagInterface $session) use ($document) {
$key = 'document_' . $document->getId();
$session->set($key, $document);
}, 'pimcore_documents');

Document\Service::saveElementToSession($document);
return new Response();
}

@@ -170,11 +170,8 @@ protected function addTranslationsData(Model\Document $document)
public function saveToSessionAction(Request $request)
{
if ($request->get('id')) {
$key = 'document_' . $request->get('id');

$session = Session::get('pimcore_documents');

if (!$document = $session->get($key)) {
$documentId = $request->get('id');
if (!$document = Model\Document\Service::getElementFromSession('document', $documentId)) {
$document = Model\Document::getById($request->get('id'));
$document = $this->getLatestVersion($document);
}
@@ -183,9 +180,7 @@ public function saveToSessionAction(Request $request)
$document->setInDumpState(true);
$this->setValuesToDocument($request, $document);

$session->set($key, $document);

Session::writeClose();
Model\Document\Service::saveElementToSession($document);
}

return $this->adminJson(['success' => true]);
@@ -198,13 +193,11 @@ public function saveToSessionAction(Request $request)
protected function saveToSession($doc, $useForSave = false)
{
// save to session
Session::useSession(function (AttributeBagInterface $session) use ($doc, $useForSave) {
$session->set('document_' . $doc->getId(), $doc);
Model\Document\Service::saveElementToSession($doc);

if ($useForSave) {
$session->set('document_' . $doc->getId() . '_useForSave', true);
}
}, 'pimcore_documents');
if ($useForSave) {
Model\Document\Service::saveElementToSession($doc, '_useForSave');
}
}

/**
@@ -220,23 +213,14 @@ protected function getFromSession($doc)
// check if there's a document in session which should be used as data-source
// see also PageController::clearEditableDataAction() | this is necessary to reset all fields and to get rid of
// outdated and unused data elements in this document (eg. entries of area-blocks)
$sessionDocument = Session::useSession(function (AttributeBagInterface $session) use ($doc) {
$documentKey = 'document_' . $doc->getId();
$useForSaveKey = 'document_' . $doc->getId() . '_useForSave';

if ($session->has($documentKey) && $session->has($useForSaveKey)) {
if ($session->get($useForSaveKey)) {
// only use the page from the session once
$session->remove($useForSaveKey);

return $session->get($documentKey);
}
}

return null;
}, 'pimcore_documents');
if ($sessionDocument = Model\Document\Service::getElementFromSession('document', $doc->getId())
&& $documentForSave = Model\Document\Service::getElementFromSession('document', $doc->getId(), '_useForSave' )) {
Model\Document\Service::removeElementFromSession('document', $doc->getId(), '_useForSave');
}
}


return $sessionDocument;
}

@@ -249,12 +233,7 @@ protected function getFromSession($doc)
*/
public function removeFromSessionAction(Request $request)
{
$key = 'document_' . $request->get('id');

Session::useSession(function (AttributeBagInterface $session) use ($key) {
$session->remove($key);
}, 'pimcore_documents');

Model\Document\Service::removeElementFromSession('document', $request->get('id'));
return $this->adminJson(['success' => true]);
}

@@ -115,9 +115,7 @@ public function saveAction(Request $request)
// save to session
$key = 'document_' . $request->get('id');

Session::useSession(function (AttributeBagInterface $session) use ($key, $page) {
$session->set($key, $page);
}, 'pimcore_documents');
Document\Service::saveElementToSession($page);

if ($request->get('task') == 'unpublish') {
$page->setPublished(false);
@@ -20,13 +20,11 @@
use Pimcore\Http\Request\Resolver\EditmodeResolver;
use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
use Pimcore\Http\RequestHelper;
use Pimcore\Model\Asset\Dao;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Service;
use Pimcore\Model\Document;
use Pimcore\Model\Staticroute;
use Pimcore\Model\Version;
use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
use Pimcore\Tool\Session;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -212,16 +210,11 @@ protected function handleAdminUserDocumentParams(Request $request, Document $doc
// why was it an object?
// $docKey = "document_" . $this->getParam("document")->getId();

$docKey = 'document_' . $document->getId();
$docSession = Session::getReadOnly('pimcore_documents');

if ($docSession->has($docKey)) {
if ($document = Document\Service::getElementFromSession('document', $document->getId())) {
// if there is a document in the session use it
$this->logger->debug('Loading preview document {document} from session', [
'document' => $document->getFullPath()
]);

// if there is a document in the session use it
$document = $docSession->get($docKey);
}
}

@@ -258,16 +251,12 @@ protected function handleAdminUserDocumentParams(Request $request, Document $doc
protected function handleEditmode(Document $document)
{
// check if there is the document in the session
$docKey = 'document_' . $document->getId();
$docSession = Session::getReadOnly('pimcore_documents');

if ($docSession->has($docKey)) {
if ($documentFromSession = Document\Service::getElementFromSession('document', $document->getId())) {
// if there is a document in the session use it
$this->logger->debug('Loading editmode document {document} from session', [
'document' => $document->getFullPath()
]);

// if there is a document in the session use it
$document = $docSession->get($docKey);
$document = $documentFromSession;
} else {
$this->logger->debug('Loading editmode document {document} from latest version', [
'document' => $document->getFullPath()
@@ -293,13 +282,8 @@ protected function handleEditmode(Document $document)
protected function handleObjectParams(Request $request)
{
// object preview
if ($request->get('pimcore_object_preview')) {
$key = 'object_' . $request->get('pimcore_object_preview');

$session = Session::getReadOnly('pimcore_objects');
if ($session->has($key)) {
/** @var Concrete $object */
$object = $session->get($key);
if ($objectId = $request->get('pimcore_object_preview')) {
if ($object = Service::getElementFromSession('object', $objectId)) {

$this->logger->debug('Loading object {object} ({objectId}) from session', [
'object' => $object->getFullPath(),
@@ -220,7 +220,6 @@ pimcore:
session:
attribute_bags:
- pimcore_admin
- pimcore_documents
- pimcore_objects
- pimcore_copy
- pimcore_gridconfig
@@ -32,7 +32,7 @@ class IFrameController extends \Pimcore\Controller\FrontendController
$language = $context["language"];
// get the current editing data, not the saved one!
$object = Service::getObjectFromSession($objectId);
$object = Service::getElementFromSession('object', $objectId);
$response = '<h1>Title for language "' . $language . '": ' . $object->getTitle($language) . "</h1>";
@@ -1,7 +1,8 @@
# Object Preview

**DEPRECATED FEATURE**
**Please use [link generators](./15_Link_Generator.md) instead, which are more powerful and flexibel and
> **DEPRECATED FEATURE**
>
> **Please use [link generators](./15_Link_Generator.md) instead, which are more powerful and flexibel and
offer the same functionality. The preview tab is automatically displayed when a link generator is defined
on the data object class.**
@@ -26,7 +27,13 @@ URL is opened in the tab. Only if you configure a preview URL in the class confi

![Object Preview](../../../img/classes-preview1.png)

The above configuration (`/news/%name_%o_id`) will result in the URL `/news/my+news+title_867655`
The above configuration (`/shop/product~p%o_id`) will result in the URL `/shop/product~p247`
(or whatever the object id is).

![Object Preview](../../../img/classes-preview2.png)

You can get the editing data (not the saved one) by calling

```php
$object = Service::getElementFromSession('object', $objectId);
```

0 comments on commit abadace

Please sign in to comment.
You can’t perform that action at this time.