Override buildForm() in another module I would like to override the buildForm() method of the Comment module in my custom module.

The method is defined in core/modules/comment/src/Form/CommentAdminOverview.php.

To achieve this, I've created myModuleName/src/Form/MyModuleNameCommentAdminOverview.php, whose first lines are the following.

namespace Drupal\myModuleName\Form; 

use Drupal\comment\CommentInterface;
use Drupal\comment\Form\CommentAdminOverview;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

class myModuleNameCommentAdminOverview  extends CommentAdminOverview {
  public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
    // ...
  }
}


In myModuleName.info.yml I have these lines. 

 dependencies:
  - comment:comment


My code is not working. What am I doing wrong? 

I have read the following, but I don't understand what I'm doing wrong. 


Need to override single Drupal 8 plugin function
Override or extend function of a contributed module
Overriding the function of a module in another module
These are the two routes for the original form class:

comment.routing.yml:

comment.admin:
  path: '/admin/content/comment'
  defaults:
    _title: 'Comments'
    _form: '\Drupal\comment\Form\CommentAdminOverview'
    type: 'new'
  requirements:
    _permission: 'administer comments'

comment.admin_approval:
  path: '/admin/content/comment/approval'
  defaults:
    _title: 'Unapproved comments'
    _form: '\Drupal\comment\Form\CommentAdminOverview'
    type: 'approval'
  requirements:
    _permission: 'administer comments'


You have to alter both routes like this

  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection-&gt;get('comment.admin')) {
      $route-&gt;setDefault('_form', '\Drupal\mymodule\Form\MyForm');
    }
  }


in a RouteSubscriber.