In my module using below code for getting url alias of given url:
$alias = \Drupal::service('path.alias_manager')->getPathByAlias($_POST['url']); But one I run Automated Review(http://pareview.sh/) in my module I am getting below warning:
16 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
How can I update above code using dependency injection? My entire class code is given below.
<?php namespace Drupal\my_module\Controller; use Drupal\Core\Controller\ControllerBase; /** * MyModule Class defines ajax callback function. */ class MyModule extends ControllerBase { /** * Callback function for ajax request. */ public function getUserContent() { $alias = \Drupal::service('path.alias_manager')->getPathByAlias($_POST['url']); $alias = explode('/', $alias); $my_module_views = views_embed_view('my_module', 'default', $alias[2]); $my_module= drupal_render($my_module_views); return array( '#name' => 'my_module_content', '#markup' => '<div class="my_module_content">' . $my_module. '</div>', ); } }