DigiComp.AttachmentViewUtility/README.md
Ferdinand Kuhl 57d3efe1df
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests Pipeline was successful
Initial version
2023-10-17 22:52:28 +02:00

1.6 KiB

DigiComp.AttachmentViewUtility

This package helps with recurring tasks of creating views for neos/flow projects.

It delivers two traits:

  1. AttachmentViewTrait creates views which returns downloadable resources
  2. EelFilenameTrait allows to set the filename with an eel expression, which got the view variables as context

They can be used together or standalone - but the filename trait does not make sense, if your view is not delivered by attachment.

The EelContext contains the String, Array and Translation - Helpers. If you need more, you can connect to the YourView::filenameEelExpressionContext signal, which gets the context as argument. That way you can extend the context with whatever you need.

Example

SimpleAttachmentView.php:

class SimpleAttachmentView extends AbstractView
{
    use AttachmentViewTrait;
    use EelFilenameTrait;

    public function __construct(array $options = [])
    {
        $this->addOptionFilenameEelExpression();
        $this->addOptionsForAttachment();
        parent::__construct($options);
    }

    protected function getAttachmentContent()
    {
        return 'Hello ' . $this->variables['name'];
    }

    protected function getAttachmentMimeType(): string
    {
        return 'text/plain';
    }
}

Controller:

class DefaultController extends ActionController {
    public function downloadAction()
    {
        $this->view->assign('name', 'World');
    }
}

Views.yaml:

-
  requestFilter: "isController('Default') && isAction('download')"
  viewObjectName: "Acme\\Vendor\\SimpleAttachmentView"
  options:
    filenameEelExpression: "'hello-' + name + '.txt"