How to deny access to a node/add/contenttype page but allow creating of that content type

Filed this issue:

Include option to include content type create page in the rabbit hole? [#3092259] | Drupal.org

A common pairing with never viewing a content type on its own is always creating that content type in-line. Would blocking access to the node/add/[contenttype] page (for content people are nonetheless allowed to create) be considered for inclusion in this module?

William Jieh asks for pretty much exactly what i need:

https://www.drupal.org/forum/support/module-development-and-code-questions/2019-07-17/how-to-disable-or-add-permission-to

and didn’t get a good answer but found documentation:

https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones

https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones#custom_access_checks

https://www.drupal.org/docs/8/api/routing-system/access-checking-on-routes/advanced-route-access-checking

https://drupal.stackexchange.com/questions/248767/how-to-set-custom-access-for-a-custom-page

https://cipix.nl/understanding-drupal-8-part-3-routing

Oh this looks like the closest analog documented:

https://drupal.stackexchange.com/questions/275503/how-to-implement-custom-access-to-node-revisions


This shows how to add routes that provide the function of node/add/content-type, but not change existing ones: https://thinkshout.com/blog/2016/07/drupal-8-routing-tricks-for-better-admin-urls/

See also the one and only commit of my stalled attempt, here: https://gitlab.com/find-it-program-locator/drutopia_findit_location/tree/prevent-direct-node-add-location

Note that the simple case of not taking any arguments is easy:

https://gitlab.com/find-it-program-locator/drutopia_findit_service_provider/commit/bde981923aa77f41e951e4f2940f07091b7c8048

Exact problem, not clearly stated permissions:

https://drupal.stackexchange.com/questions/211448/how-to-change-permission-for-dynamic-routes

Ah no i don’t have the exact problem; i’m trying to do what Neha has already solved. So i can do it like this, in src/Routing/RouteSubscriber.php:

namespace Drupal\drutopia_findit_service_provider\Routing;

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

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    // Always deny access to '/node/add' listing of available content to create.
    if ($route = $collection->get('node.add_page')) {
      // Note that the second parameter of setRequirement() is a string.
      $route->setRequirement('_permission', 'administer nodes');
    }
  }

}

This depends on having a modulename.services.yml file with this:

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