Anyone else having trouble getting custom handlers to wrok on D8 entities?

1.4k views Asked by At

I'm starting out with Drupal 8 and until now I have been quite impressed with all the new functionality. However, I have been trying to write my own entity and I'm running into trouble:

This is the entity definition:

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Drupal\entitytest\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Defines the Candidate Entity
 * 
 * @ingroup entitytest
 * 
 * @ContentEntityType(
 *    id="entitytest_AuditionCandidate",
 *    label=@Translation("Candidate"),
 *    base_table="candidate",
 *    
 *    handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\entitytest\Entity\Controller\CandidateListBuilder",
 *     "form" = {
 *       "add" = "Drupal\Core\Entity\ContentEntityForm",
 *       "edit" = "Drupal\Core\Entity\ContentEntityForm",      
 *       "delete" = "Drupal\EntityTest\Form\CandidateDeleteForm",
 *        },   
 *    },
 *    admin_permission="administer candidates",
 *    entity_keys={
 *      "id"="id",
 *      "label"="lastname",   
 *   },
 *    links = {
 *     "canonical" = "/AuditionCandidate/view/{entitytest_AuditionCandidate}",
 *     "edit-form" = "/AuditionCandidate/edit/{entitytest_AuditionCandidate}",
 *   },
 * )
 */
class Candidate extends ContentEntityBase {
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
     $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Contact entity.'))
      ->setReadOnly(TRUE);

     $fields['lastname'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Last Name'))
      ->setDescription(t('The name of the Contact entity.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

     $fields['firstname'] = BaseFieldDefinition::create('string')
      ->setLabel(t('First Name'))
      ->setDescription(t('The name of the Contact entity.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

     return $fields;
  }
}

So I am trying to edit the Deleteform from this entity. I have created a file under /modules/custom/EntityTest/src/Form/CandidateFormDelete.php

The code in this file is as follows:

<?php
namespace Drupal\EntityTest\Form;

use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class CandidateDeleteForm extends ContentEntityConfirmFormBase {
  public function getQuestion() {
    return $this->t('Are you sure?');
  }

  public function getCancelUrl() {
    return new Url('entity.entitytest_AuditionCandidate.collection');
  }

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

  }
}

I have also added a route for the delete form:

entity.entitytest_AuditionCandidate.delete_form:
    path: 'AuditionCandidate/delete/{entitytest_AuditionCandidate}'
    defaults:
        _entity_form: entitytest_AuditionCandidate.delete
        _title: 'Delete Candidate'
    requirements:
        _permission: 'administer candidates'

But when I try to open /AuditionCandidate/delete/1 I'm getting the following error message:

Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "entitytest_AuditionCandidate" entity type did not specify a "delete" form class. in Drupal\Core\Entity\EntityManager->getFormObject() (line 309 of core/lib/Drupal/Core/Entity/EntityManager.php).

It just doesn't seem to make sense since I have definined a class for the deleteform.

Anyone who can see what I am missing? It's possibly just a typo but I have been staring at it for quite some time now and I just can't figure it out.

Matt.

1

There are 1 answers

0
Bon Kim On

Drupal 8 implements the PSR-4 standard for package-based PHP namespace autoloading. In that case the name of your class file doesn't correspond to the name of the actual class used. The file name should also be "CandidateDeleteForm.php" instead of "CandidateFormDelete"

That's the reason why you are getting that exception. For more about that subject read: https://www.drupal.org/node/2156625