Show-last-facebook-post plugin

The plugin should preserve the user privacy and therefore load the content from facebook and display it as content element.

Before I start building such an plugin, I want to ask a couple of questions:

  • Is there maybe already such kind of plugin?
  • Is it the right approach to create a plugin or should it be a viewhelper?
  • Can I use the flow cache management to prevent sending to many requests to the facebook server?

Thank you in advance.

Hi Klaus,

I haven’t come across such a plugin, but that doesn’t necessarily mean there is none. You might browse through the packages which depend on Neos on packagist.org.

My gut feeling says, it doesn’t have to be a plugin really, because it only (presumably) has only one action (show last Facebook post). So you might simply create a new NodeType and use a custom view helper. In that case, the content element based on that node type will behave like all other content elements, which means it follows the general caching rules. You can set an expiry time and by that minimize the amount of requests to Facebook.

Hey Klaus,

I built a Nodetype / ViewHelper to display latest events from facebook which you can find here: https://github.com/daniellienert/DPSG.CampGround/blob/master/Classes/DPSG/CampGround/ViewHelpers/Facebook/EventsViewHelper.php

Caching works fine as Robert just said.

Cheers,
Daniel

Thank you guys for your kind answers, but in the mean time I already built a fb plugins, which works quit good. I felt that a plugin is much cleaner than a viewhelper, but I think this a matter of opinion.

I faced another issue for which I am still looking for a clean solution: Fb images. How can I display those to the user without revealing there privacy? Currently I am using a combination of rewrite rules and a php script which downloads the given image to the public resource folder. But I found no solution how to use the media viewhelper with those “static” resources and I feel uneasy with my self developed image cache.

I also tried to import the fb image in my controller with this snippet:

$newResource = $this->resourceManager->importResource('http://......');
$image = new \TYPO3\Media\Domain\Model\Image($newResource);
$this->view->assign('image', $image);

But I get an error about changed content in a http get request or something similiar.

Any idea, how to use the the media viewhelper for static resources or how to cache remote content?

Flow implement HTTP safe request, aka “a GET request should not change anything in your database”. So in a GET request you must explicitly allow persistence for each entity, you can use the PersistenceManager for this, check the ThumbnailService::getThumbnail (in the Media package)

This snippet should work:

// Allow image to be persisted even if this is a "safe" HTTP request:
$this->persistenceManager->whiteListObject($image);
$this->persistenceManager->whiteListObject($image->getResource());

Wuhuuu, it works like a charm.

At first it sounds a little bit strange to allow persistence for that image, but at the end it makes sense to me.

In case someone is interested in the code, here you go:

1 Like

Congrats and well done, why not building a small Package and publish it on Packagist ? Sounds like a common issue, let’s make it visible.

I write a small suggestion in the gist

Congrats and well done, why not building a small Package and publish it on Packagist ? Sounds like a common issue, let’s make it visible.

I am a little bit afraid to maintain such a package, because I am already maintaining a couple of projects, but one more or less should not matter :smile:.

If the scope is simple maintenance is easy :wink:

If the scope is simple maintenance is easy

That’s right.

Do you know how to get the image by filename, because currently I see the image multiple times in the media browser. I found only a solution by using the sha1 value.

I am currently facing a new issue where I need some help.

To avoid import of the same image, I tried the following:

$sha1 = sha1_file($src);
$resource = $this->resourceManager->getResourceBySha1($sha1);

if ($resource === null) {
  $resource = $this->resourceManager->importResource($src);
}

$image = new \TYPO3\Media\Domain\Model\Image($resource);
$this->view->assign('image', $image);

But after the image is imported, the site crashes with the error:

An exception was thrown while Neos tried to render your page
Node workspace not found in allowed workspaces, this could result from a detached workspace entity in the context.
page<TYPO3.Neos:Page>/
 body<TYPO3.TypoScript:Template>/
  content/
   commonSidebar<TYPO3.TypoScript:Collection>/
For a full stacktrace, open Data/Logs/Exceptions/20151012124120158262.txt

And the system_development.log shows the following:

15-10-12 12:41:21 13371      DEBUG     Flow                 Session: Resumed session with id P0KrCzr6TwPategnqnvvv2z59R1UptSh which was inactive for 3 minutes. (218s)
15-10-12 12:41:21 13371      DEBUG     Flow                 Router route(): A cached Route with the cache identifier "f86901d39c73b00257d482911385aa8a" matched the path "index.html".
15-10-12 12:41:21 13371      DEBUG     Flow                 CSRF: No token required, safe request
15-10-12 12:41:21 13371      DEBUG     Flow                 CSRF: No token required, safe request
15-10-12 12:41:22 13371      DEBUG     Flow                 Successfully imported file "/var/www/Neos-2.0/Data/Temporary/Development/ProcessedImage-561baa725f5f8.jpg?oh=7dd00b577204d7c66995e8f3c6287b56&oe=5699a55b" into the resource collection "persistent" (storage: defaultPersistentResourcesStorage, a TYPO3\Flow\Resource\Collection. SHA1: 51ce9774471a46611bb42447e846f998f7d5d544)
15-10-12 12:41:22 13371      DEBUG     Flow                 FileSystemSymlinkTarget: Published file. (target: localWebDirectoryPersistentResourcesTarget, file: 51ce9774471a46611bb42447e846f998f7d5d544/12140842_1017635598256867_3362333541560488648_n-150x113.jpg?oh=7dd00b577204d7c66995e8f3c6287b56&oe=5699a55b)
15-10-12 12:41:22 13371      CRITICAL  DBAL                 Uncaught exception in line 116 of /var/www/Neos-2.0/Packages/Libraries/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php: An exception occurred while executing 'INSERT INTO typo3_media_domain_model_asset (persistence_object_identifier, lastmodified, title, caption, resource, dtype) VALUES (?, ?, ?, ?, ?, ?)' with params ["d2d15d84-e330-cd60-287a-066c30bed2e3", "2015-10-12 12:41:22", "", "", "145535aa-63d6-fa10-2a18-58976eb11258", "typo3_media_image"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '145535aa-63d6-fa10-2a18-58976eb11258' for key 'UNIQ_B8306B8EBC91F416' - See also: 2015101212412044fe0d.txt
    previousException => Uncaught exception #23000 in line 165 of /var/www/Neos-2.0/Packages/Libraries/doctrine/dbal/lib/Doctrine/DBAL/Statement.php: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '145535aa-63d6-fa10-2a18-58976eb11258' for key 'UNIQ_B8306B8EBC91F416'

15-10-12 12:41:22 13371      CRITICAL  TYPO3CR              Uncaught exception #1413902143 in line 208 of /var/www/Neos-2.0/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/TYPO3_TYPO3CR_Domain_Repository_NodeDataRepository.php: Node workspace not found in allowed workspaces, this could result from a detached workspace entity in the context. - See also: 20151012124120717eff.txt
15-10-12 12:41:22 13371      CRITICAL  TYPO3CR              Uncaught exception #1413902143 in line 484 of /var/www/Neos-2.0/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/TYPO3_TYPO3CR_Domain_Repository_NodeDataRepository.php: Node workspace not found in allowed workspaces, this could result from a detached workspace entity in the context. - See also: 20151012124120f0e8aa.txt
15-10-12 12:41:22 13371      CRITICAL  TYPO3CR              Uncaught exception #1413902143 in line 484 of /var/www/Neos-2.0/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/TYPO3_TYPO3CR_Domain_Repository_NodeDataRepository.php: Node workspace not found in allowed workspaces, this could result from a detached workspace entity in the context. - See also: 20151012124120158262.txt
15-10-12 12:41:22 13371      ERROR     Flow                 persistAll() skipped flushing data, the Doctrine EntityManager is closed. Check the logs for error message.
15-10-12 12:41:22 13371      DEBUG     Flow                 Not removing storage data of resource 12140842_1017635598256867_3362333541560488648_n-150x113.jpg?oh=7dd00b577204d7c66995e8f3c6287b56&oe=5699a55b (51ce9774471a46611bb42447e846f998f7d5d544) because it is still in use by 2 other Resource object(s).
15-10-12 12:41:22 13371      CRITICAL  ORM                  Uncaught exception in line 708 of /var/www/Neos-2.0/Packages/Libraries/doctrine/orm/lib/Doctrine/ORM/EntityManager.php: The EntityManager is closed. - See also: 201510121241203fa471.txt

I found out, that this only occurs, if I try to generate a thumbnail from the returned image.

{media:uri.image(asset: image, maximumHeight: 150, maximumWidth: 150)}

This drives me crazy. I don’t have a glue, why this fails. Please give me some assistance.

Ok, I think I figured it out:

$image = $this->assetRepository->findOneByResourceSha1($sha1);

if ($image === null) {
    $resource = $this->resourceManager->importResource($src);
    $image = new \TYPO3\Media\Domain\Model\Image($resource);
}

Now I can start transfering my code to a plugin. Thanks for your help.