1

I'm just trying to clone a d7 site in 8. My first step was to use drupal console to generate a custom entity. Everything is working fine but I want to change the title of the form to "Create project" instead of the default "Add project".

I don't see "Add " anywhere except for module.links.action.yml, but that is just the local action - it doesn't change the name of the form.

The root of the directory looks like:

config
  schema
    project_entity_type.schema.yml
src
  Entity
    ...several classes
  Form
    ...several classes
  templates
    ...couple template files
  Tests
    LoadTest.php
module.page.inc
module.links.action.yml
module.links.menu.yml
module.links.tasks.yml
module.module
module.permissions.yml
2
  • Did you find a solution?
    – Saulius
    Commented Apr 11, 2017 at 11:08
  • @Saulius This was just a play-around project so it never made it past a demo phase; however, I posted my solution below - not sure if it's the best way or not. Commented Apr 12, 2017 at 11:49

1 Answer 1

2

The answer comes from here: https://www.drupal.org/node/2187643.

Note: change "module" in the below code to the name of your module.

The first step is to register an event subscriber service. In module.services.yml:

services:
  module.route_subscriber:
    class: Drupal\module\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

Next, create your class RouteSubscriber and extend RouteSubscriberBase:

  namespace Drupal\module\Routing;

  use Drupal\Core\Routing\RouteSubscriberBase;
  use Symfony\Component\Routing\RouteCollection;

  /**
   * Subscribe to the Route to change page titles.
   */
  class RouteSubscriber extends RouteSubscriberBase {

    private function alterRoutes(RouteCollection $collection) {
      if ($route = $collection->get('entity.project_entity.add_form')) {
        // set the title (or override '_title_callback' below)
        $route->setDefault('_title', 'Create Project Request');
        // unset the _title_callback; alternatively you could override it here
        $route->setDefault('_title_callback', '');
      }
    }
  }

Note that you receive a RouteCollection in alterRoutes and then using the $collection-get() will return an instance of Route. The documentation for Route can be found here: http://api.symfony.com/2.7/Symfony/Component/Routing/Route.html

If you're having trouble figuring out what your routes are, the excellent drupal console drupal route:debug tool will help you.

1
  • 1
    To bad this doesn't seems to work on the html <title>, works great for the page title 'tho. :)
    – Esolitos
    Commented Dec 5, 2017 at 7:19

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.