Example Plugin

In Neos you can enrich your nodes with PHP. In this example you will primarily see how to link PHP scripts to your node and output them in the html template. (If you want to see how to link Fusion and PHP look at Example Eel Helper) I'm also testing how to manipulate the repositories of Neos with PHP.  I read the first name, last name and user name and try to save the new data. This way you can also reset a user's password. But please make sure that I am still working on this example at the moment.

IMPORTANT: This article deals with the topic in fluid. Fluid is no longer best practice. We are working on rewriting the article in AFX.


Get Username Username
getFirstName (before set property): First name Last name
getFirstName (after set property): Franz Last name


Look what's behind it:

# Example Plugin
'Arsors.Neos:ExamplePlugin':
superTypes:
'Neos.Neos:Plugin': true
ui:
label: 'Example Plugin'
icon: 'icon-list'

prototype(Arsors.Neos:ExamplePlugin) < prototype(Neos.Neos:Plugin) {

package = 'Arsors.Neos'
controller = 'ExamplePlugin'
action = 'index'

}

<?php
namespace Arsors\Neos\Controller;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;

use Neos\Flow\Security\Context;
use Neos\Neos\Domain\Service\UserService;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;

use Neos\Flow\Security\Authentication\TokenInterface;

class ExamplePluginController extends ActionController
{
/**
* @Flow\Inject
* @var Context
*/
protected $securityContext;

/**
* @Flow\Inject
* @var UserService
*/
protected $userService;

/**
* project overview page (index)
* @return mixed
*/
public function indexAction() //TokenInterface $authenticationToken
{

// Get Account of User
$user = $this->userService->getCurrentUser();

// If User loggedin...
if ($user) {


/////////////////////////////////////////////
// Set Account Identifier (Name to Login)
$username = $this->securityContext->getAccount();

/*
$username->setAccountIdentifier("Arsors");
// TODO Save setAccountIdentifier change
// TODO THIS IS NOT WORKING !!
$authenticationToken = $this->securityContext->getAuthenticationTokens()['Neos.Neos:Backend'];
$authenticationToken->setAuthenticationStatus(TokenInterface::AUTHENTICATION_SUCCESSFUL);
$authenticationToken->setAccount($username);
$this->view->assign('authenticationToken', $authenticationToken);
*/

// Get Account Identifier (Name to Login)
$username = $this->userService->getUsername($user);
$this->view->assign('username', $username);

/////////////////////////////////////////////
// Set Password
//$this->userService->setUserPassword($user, "YourPasswordGetHashedFromNeos!");

/////////////////////////////////////////////
// Set FirstName
$firstNameBeforeSet = $user->getName()->getFirstName();
$user->getName()->setFirstName('Franz');
//$this->userService->updateUser($user); // Uncommend this line to save the new username, remember to escape $this->persistenceManager->persistAll(); as well
// Get FirstName & LastName
$firstNameAfterSet = $user->getName()->getFirstName();
$lastName = $user->getName()->getLastName();
$this->view->assign('firstNameBeforeSet', $firstNameBeforeSet." ".$lastName);
$this->view->assign('firstNameAfterSet', $firstNameAfterSet." ".$lastName);

// Give me rights to write, mofucker!!
//$this->persistenceManager->persistAll();



} else {
// Return a failure
//$this->view->assign('user', "No User found!");

// Because you are probably not logged in I set the variable manually here so that an output takes place
$username = "Username";
$firstNameBeforeSet = "First name";
$firstNameAfterSet = "Franz";
$lastName = "Last name";

$this->view->assign('username', $username);
$this->view->assign('firstNameBeforeSet', $firstNameBeforeSet." ".$lastName);
$this->view->assign('firstNameAfterSet', $firstNameAfterSet." ".$lastName);

}

}

}

<hr>
Get Username <span class="badge badge-secondary">{username}</span>
<hr>
getFirstName (before set property): <span class="badge badge-secondary">{firstNameBeforeSet}</span><br />
getFirstName (after set property): <span class="badge badge-secondary">{firstNameAfterSet}</span><br />
<hr>

Because a PHP script in Neos is prevented for security reasons we have to extend the user rights. So we allow Neos.Flow:Everybody (every visitor of the website/app) to access our script. Of course, user rights adaptations should be treated with caution.

privilegeTargets:
  'Neos\Flow\Security\Authorization\Privilege\Method\MethodPrivilege':
    'Arsors.Neos:ExamplePlugin':
      matcher: 'method(Arsors\Neos\Controller\ExamplePluginController->(.*)Action())'

roles:
  'Neos.Flow:Everybody':
    privileges:
      -
        privilegeTarget: 'Arsors.Neos:ExamplePlugin'
        permission: GRANT

I have overwritten the default HTML-Template path of custom plugins in Neos here to have more of an association with a NodeType.

-
  requestFilter: 'isPackage("Arsors.Neos") && isController("ExamplePlugin") && isAction("index")'
  options:
    templatePathAndFilename: 'resource://Arsors.Neos/Private/Templates/NodeTypes/ExamplePlugin.html'
Last cache from 2020-10-06 at 08:29:19 (Example Eel Helper)