1

This is my Config entity:

 * @ConfigEntityType(
 *   id = "myentity",
 *   label = @Translation("My Entity"),
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigEntityStorage",
 *     "list_builder" = "Drupal\mymodule\Controller\MyEntityListBuilder",
 *     "form" = {
 *       "default" = "Drupal\mymodule\Form\MyEntityForm",
 *       "delete" = "Drupal\mymodule\Form\MyEntityDeleteForm",
 *     },
 *   },
 *   admin_permission = "administer mymodule",
 *   config_prefix = "myentity",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "url" = "url",
 *     "status" = "status"
 *   },
 *   links = {
 *     "canonical" = "mymodule.myentity_info",
 *     "add-form" = "mymodule.myentity_add",
 *     "edit-form" = "mymodule.myentity_edit",
 *     "delete-form" = "mymodule.myentity_delete",
 *   }
 * )
 */

The MyEntityDeleteForm:

/**
 * @file
 * Contains \Drupal\mymodule\Form\MyEntityDeleteForm.
 */

namespace Drupal\mymodule\Form;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Url;

/**
 * Defines a confirmation form for deletion of a custom menu.
 */
class MyEntityDeleteForm extends EntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to delete the myentity %title?', array('%title' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, array &$form_state) {
    $this->entity->delete();
    drupal_set_message($this->t('The entity %title has been deleted.', array('%label' => $this->entity->label())));
    $form_state['redirect_route'] = $this->getCancelRoute();
  }

  public function getConfirmText() {
    return $this->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelRoute() {
    return $this->entity->urlInfo('canonical');
  }
}

The route defined in routing.yml:

mymodule.myentity_delete:
  path: '/admin/config/search/mymodule/myentity/{myentity}/delete'
  defaults:
    _entity_form: 'mymodule_myentity.delete'
  requirements:
    _entity_access: 'mymodule_myentity'

I'm getting this error while trying to delete the entity:

Notice: Undefined offset: 1 in Drupal\Core\Entity\EntityAccessCheck->access() (line 46 of core/lib/Drupal/Core/Entity/EntityAccessCheck.php).
Drupal\Core\Entity\EntityAccessCheck->access(Object, Object, Object)
call_user_func_array(Array, Array)
Drupal\Core\Access\AccessManager->performCheck('access_check.entity', Object, Object, Object)
Drupal\Core\Access\AccessManager->checkAll(Array, Object, Object, Object)
Drupal\Core\Access\AccessManager->check(Object, Object, Object)
Drupal\Core\EventSubscriber\AccessSubscriber->onKernelRequestAccessCheck(Object, 'kernel.request', Object)
call_user_func(Array, Object, 'kernel.request', Object)
Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'kernel.request', Object)
Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', Object)
Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\HttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)

And this message on the page:

Access denied
You are not authorized to access this page.
3
  • The message you've got there isn't an exception (it's just a notice). We'll need to see the exception message to help
    – Clive
    Jul 9, 2014 at 10:44
  • @Clive: Please see my updated question. I made a few edits.
    – xan
    Jul 9, 2014 at 10:46
  • @Mołot: I'm taking help from here: drupal.org/node/1809494
    – xan
    Jul 9, 2014 at 10:51

1 Answer 1

1

If you look at EntityAccessCheck::access() you'll see this:

$requirement = $route->getRequirement('_entity_access');
list($entity_type, $operation) = explode('.', $requirement);

The second line is causing the notice, and your access check to subsequently fail. Basically, the access method is expecting (requiring, in fact) your permission to contain an entity type and access operation, separated by a ..

If you take the node module's routing file as an example:

node.delete_confirm:
  path: '/node/{node}/delete'
  defaults:
    _entity_form: 'node.delete'
    _title: 'Delete'
  requirements:
    _entity_access: 'node.delete'
  options:
    _node_operation_route: TRUE

I would suggest you just need to change your route to match:

mymodule.myentity_delete:
  path: '/admin/config/search/mymodule/myentity/{myentity}/delete'
  defaults:
    _entity_form: 'mymodule_myentity.delete'
  requirements:
    _entity_access: 'mymodule_myentity.delete'
4
  • I'd already done this. It didn't show any warnings/errors but simply shows an "Access denied page".
    – xan
    Jul 9, 2014 at 11:08
  • Don't forget you'll need to rebuild the cache after altering the route..
    – Clive
    Jul 9, 2014 at 11:08
  • Yeah. I did that. But still, the "Access denied" page.
    – xan
    Jul 9, 2014 at 11:11
  • Presumably the user you're trying to delete the entity which doesn't have access permissions to do so
    – Clive
    Jul 9, 2014 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.