0

Fatal Error: Fatal error: Class 'Drupal\custom_avatar\CustomAvatarHtmlRouteProvider' not found in /Sites/drupal8/core/lib/Drupal/Core/Entity/EntityTypeManager.php on line 252

I am creating config Entity type and below is structure of my module shown in screenshot:

enter image description here

I below code I have defined below routing so that I don't have to define each of these routes statically in our module's routing.yml file. My class extends AdminHtmlRouteProvider :

"route_provider" = { "html" = "Drupal\custom_avatar\CustomAvatarHtmlRouteProvider", },

<?php

namespace Drupal\custom_avatar\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;


/**
 * Defines the Avatar profile entity.
 *
 * @ConfigEntityType(
 *   id = "custom_avatar",
 *   label = @Translation("Avatars for user profiles"),
 *   handlers = {
 *     "list_builder" = "Drupal\custom_avatar\CustomAvatarListBuilder",
 *     "form" = {
 *       "add" = "Drupal\custom_avatar\Form\CustomAvatarForm",
 *       "edit" = "Drupal\custom_avatar\Form\CustomAvatarForm",
 *       "delete" = "Drupal\custom_avatar\Form\CustomAvatarFormDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\custom_avatar\CustomAvatarHtmlRouteProvider",
 *     },
 *   },
 *   config_prefix = "custom_avatar",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/custom_avatar/{custom_avatar}",
 *     "add-form" = "/admin/structure/custom_avatar/add",
 *     "edit-form" = "/admin/structure/custom_avatar/{custom_avatar}/edit",
 *     "delete-form" = "/admin/structure/custom_avatar/{custom_avatar}/delete",
 *     "collection" = "/admin/structure/custom_avatar"
 *   }
 * )
 */

class CustomAvatar extends ConfigEntityBase implements CustomAvatarInterface {
  /**
   * The Avatar profile ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The Avatar profile label.
   *
   * @var string
   */
  protected $label;
}




<?php

namespace Drupal\custom_avatar;

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;

class CustomAvatarHtmlRouteProvider extends AdminHtmlRouteProvider {

  /**
   * {@inheritdoc}
   */
  public function getRoutes(EntityTypeInterface $entity_type) {
    $collection = parent::getRoutes($entity_type);

    $entity_type_id = $entity_type->id();

    if ($collection_route = $this->getCollectionRoute($entity_type)) {
      $collection->add("entity.{$entity_type_id}.collection", $collection_route);
    }

    return $collection;
  }

  /**
   * Gets the collection route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getCollectionRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('collection'));
      $route
        ->setDefaults([
          '_entity_list' => $entity_type_id,
          // Make sure this is not a TranslatableMarkup object as the
          // TitleResolver translates this string again.
          '_title' => (string) $entity_type->getLabel(),
        ])
        ->setRequirement('_permission', $entity_type->getAdminPermission())
        ->setOption('_admin_route', TRUE);

      return $route;
    }
  }

}

Please suggest what is cause of error, the module gets installed without any error but when I clear all cache it shows error and the routs are not build using this way(page not found). I want to try using this class to build dynamic admin (canonical, add, edit, and delete forms for entities ) routs rather than defining in routing.yml

Thanks!

2

Unless you’re doing something custom, your classes need to be under ./src to be discovered. By default, MODULE/src/Foo.php is mapped to the Drupal\MODULE\Foo namespace and so on.

Move the two classes out of the root of your module and into ./src, clear cache, and try again.

See PSR4 namespaces and autoloading in Drupal 8 for full details.

  • Oh I see I made them out of src, thanks it worked very well ! – jas Jan 31 '18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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