The WidgetContext can be fetched from the current request as internal argument __widgetContext,
and is thus available throughout the whole sub-request of the widget. It is used internally
by various ViewHelpers (like , , ),
to get knowledge over the current widget's configuration.
It is a purely internal class which should not be used outside of Fluid.
/** * @test */ public function viewHelperChildNodesCanBeReadAgain() { $viewHelperChildNodes = $this->createMock(RootNode::class); $renderingContext = $this->createMock(RenderingContextInterface::class); $this->widgetContext->setViewHelperChildNodes($viewHelperChildNodes, $renderingContext); $this->assertSame($viewHelperChildNodes, $this->widgetContext->getViewHelperChildNodes()); $this->assertSame($renderingContext, $this->widgetContext->getViewHelperChildNodeRenderingContext()); }
/** * @test */ public function processRequestShouldSetWidgetConfiguration() { /** @var \Neos\Flow\Mvc\ActionRequest $mockActionRequest */ $mockActionRequest = $this->createMock(\Neos\Flow\Mvc\ActionRequest::class); $mockResponse = $this->createMock(\Neos\Flow\Http\Response::class); $httpRequest = Request::create(new Uri('http://localhost')); $mockActionRequest->expects($this->any())->method('getHttpRequest')->will($this->returnValue($httpRequest)); $expectedWidgetConfiguration = array('foo' => uniqid()); $widgetContext = new WidgetContext(); $widgetContext->setAjaxWidgetConfiguration($expectedWidgetConfiguration); $mockActionRequest->expects($this->atLeastOnce())->method('getInternalArgument')->with('__widgetContext')->will($this->returnValue($widgetContext)); $abstractWidgetController = $this->getAccessibleMock(\Neos\FluidAdaptor\Core\Widget\AbstractWidgetController::class, array('resolveActionMethodName', 'initializeActionMethodArguments', 'initializeActionMethodValidators', 'mapRequestArgumentsToControllerArguments', 'detectFormat', 'resolveView', 'callActionMethod')); $abstractWidgetController->_set('mvcPropertyMappingConfigurationService', $this->createMock(\Neos\Flow\Mvc\Controller\MvcPropertyMappingConfigurationService::class)); $abstractWidgetController->processRequest($mockActionRequest, $mockResponse); $actualWidgetConfiguration = $abstractWidgetController->_get('widgetConfiguration'); $this->assertEquals($expectedWidgetConfiguration, $actualWidgetConfiguration); }
/** * @test */ public function setChildNodesAddsChildNodesToWidgetContext() { $this->widgetContext = new \Neos\FluidAdaptor\Core\Widget\WidgetContext(); $this->viewHelper->injectWidgetContext($this->widgetContext); $node1 = $this->createMock(AbstractNode::class); $node2 = $this->getMockBuilder(TextNode::class)->disableOriginalConstructor()->getMock(); $node3 = $this->createMock(AbstractNode::class); $rootNode = new RootNode(); $rootNode->addChildNode($node1); $rootNode->addChildNode($node2); $rootNode->addChildNode($node3); $renderingContext = $this->createMock(RenderingContextInterface::class); $this->viewHelper->_set('renderingContext', $renderingContext); $this->viewHelper->setChildNodes(array($node1, $node2, $node3)); $this->assertEquals($rootNode, $this->widgetContext->getViewHelperChildNodes()); }
/** * Stores the WidgetContext inside the Context, and sets the * AjaxWidgetIdentifier inside the Widget Context correctly. * * @param WidgetContext $widgetContext * @return void * @Flow\Session(autoStart=true) */ public function store(WidgetContext $widgetContext) { $ajaxWidgetId = $this->nextFreeAjaxWidgetId++; $widgetContext->setAjaxWidgetIdentifier($ajaxWidgetId); $this->widgetContexts[$ajaxWidgetId] = $widgetContext; }
/** * The widget identifier is unique on the current page, and is used * in the URI as a namespace for the widget's arguments. * * @return string the widget identifier for this widget * @return void */ private function initializeWidgetIdentifier() { $widgetIdentifier = $this->hasArgument('widgetId') ? $this->arguments['widgetId'] : strtolower(str_replace('\\', '-', get_class($this))); $this->widgetContext->setWidgetIdentifier($widgetIdentifier); }