0

I'm not certain if I'm over-complicating something or just missing something or other.

Essentially, and in addition to a separate admin view of my custom entities, I want to assign a URL to them at the root of the site (i.e. /entity01).

I tried creating a URL property, but getting a route to respond to that is proving challenging. I tried adding a route like:

my_entities.entity_page:
  path: '/{entity_url}'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController::view'
    _title: 'Hello'
  options:
    parameters:
      entity_url:
        type: entity_url
  requirements:
    _permission: 'access content'

I have a custom a custom parameter converter set up as in:

https://www.drupal.org/docs/8/api/routing-system/implementing-custom-parameter-converters

... and in the convert method, I can use an injected service to load my entity, etc. But in the applies method, I would like to see if it even matches any of my entities and return true or false appropriately, but the applies method doesn't seem to have the value of the URL parameter like the convert method does.

And even if I solve that, the above example only works with a path like:

path: '/my/{entity_url}'

If I remove the /my it doesn't work - I assume because it is conflicting with another route?

Thanks for any help!

  • 2
    I believe this was asked recently, but your route cannot start with a parameter like that. – Kevin Feb 7 at 21:52
3

I think you can do this with the pathauto (path alias?) module. Go to the Admin -> Config -> Search / Metadata -> URL Aliases. In the settings tab, enable your custom entity type.

When you edit them, you should have a URL field added. You can also use the patterns to define default paths if you want.

I did a quick test with an ECK custom entity and it worked.

  • Wow ... I can't believe I didn't look there. I really thought I'd have to put the URL alias field in some manner myself, but it took care of it. Only 2 more issues I'll have: I really wanted a separate (or additional) view of the entity from the admin view (I suppose I can give this one a different theme) and I have some sub-pages I'll need off of this (i.e. /{entity_url}/subpage. I may have a lead on the latter issue. Thanks! – Delford Chaffin Feb 7 at 23:35

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.