/** * Returns all the entity actions for the provided entity object. The output will consist of an <ul> containing separate <li>'s for each entity * action. By default, this method only outputs entity actions that the current user is currently authorized for and that are currently enabled. * If no entity actions are registered for the provided entity's class, an empty string will be returned unless the div option is specified. * @param Entity $entity * The entity object to retrieve the entity actions for. * @param array $options * An array of options that influence the output generated by this method. * `div` * Set to true in order to wrap the output in a div. If you provide a string value with this key, it will become an additional class * of the div. Defaults to false. * `notAuthorized` * Set to true in order to output entity actions that the current user is not authorized to use. Those entity action's <li>s will * additionally have the 'not-authorized' class. Defaults to false. * `disabled` * Set to true in order to output entity actions that are currently disabled. Those entity action's <li>s will additionally have the * 'disabled' class. * @return string Returns the produced output. */ public function getActions(Entity $entity, array $options = []) { $options = array_merge(['div' => false, 'notAuthorized' => false, 'disabled' => false], $options); $entityActions = EntityActionManager::get($entity); $listItems = ''; foreach ($entityActions as $entityAction) { $userId = $this->request->session()->read('Auth.User.id'); $authorized = $entityAction->isAuthorized($entity, $userId, $this->request); $enabled = $entityAction->isEnabled($entity); if (!$authorized && !$options['notAuthorized'] || !$enabled && !$options['disabled']) { continue; } $listItemClass = trim(sprintf('entity-action %s %s', $entityAction->getClass(), $this->getAdditionalClasses($authorized, $enabled))); $link = $this->Html->link($entityAction->getLabel(), $entityAction->getUrl($entity)); $listItems .= $this->Html->tag('li', $link, ['class' => $listItemClass]); } $output = empty($listItems) ? '' : $this->Html->tag('ul', $listItems, ['class' => 'entity-actions']); if ($options['div']) { $divClass = 'entity-actions'; if ($options['div'] !== true) { $divClass .= sprintf(' %s', $options['div']); } $output = $this->Html->div($divClass, $output); } return $output; }
/** * @runInSeparateProcess * @preserveGlobalState disabled * @expectedException EntityActions\Exception\MissingEntityActionRegistryException */ public function testGetThrowsExceptionWhenRegistryIsNotSet() { EntityActionManager::get('Cake\\ORM\\Entity'); }