{"id":55330,"date":"2022-07-22T12:16:31","date_gmt":"2022-07-22T06:46:31","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55330"},"modified":"2022-07-22T12:16:31","modified_gmt":"2022-07-22T06:46:31","slug":"how-to-create-custom-drush-command-in-drupal","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-create-custom-drush-command-in-drupal\/","title":{"rendered":"How to create custom Drush command in Drupal"},"content":{"rendered":"<p>In this tutorial se are going learn how to create custom Drush command in drupal. For that we need to create a custom module. The file structure will be like below<\/p>\n<pre>drush_command_example\r\n\u00a0 \u00a0Commands\r\n\u00a0 \u00a0 \u00a0 CustomCommands.php\r\n\u00a0  drush_command_example.info\r\n\u00a0 drush.services.yml\r\n\r\n\r\n<\/pre>\n<p>1. <strong>drush_command_example.info file<\/strong><\/p>\n<pre><code class=\"language-twig hljs http\"><span class=\"hljs-attribute\">name<\/span>: <span class=\"hljs-string\">Drush Command Example<\/span>\r\n<span class=\"hljs-attribute\">description<\/span>: <span class=\"hljs-string\">This is our custom drush command.<\/span>\r\n<span class=\"hljs-attribute\">core<\/span>: <span class=\"hljs-string\">8.x<\/span>\r\n<span class=\"hljs-attribute\">core_version_requirement<\/span>: <span class=\"hljs-string\">^8 || ^9<\/span>\r\n<span class=\"hljs-attribute\">type<\/span>: <span class=\"hljs-string\">module<\/span>\r\n<span class=\"hljs-attribute\">package<\/span>: ttn<\/code><\/pre>\n<p><strong>2. drush.services.yml file.<\/strong><\/p>\n<pre><code class=\"language-twig hljs css\"><span class=\"hljs-tag\">services<\/span>:\r\n\u00a0 <span class=\"hljs-tag\">drush_command_example<\/span><span class=\"hljs-class\">.commands<\/span>:\r\n\u00a0 \u00a0 <span class=\"hljs-tag\">class<\/span>: \\<span class=\"hljs-tag\">Drupal<\/span>\\<span class=\"hljs-tag\">drush_command_example<\/span>\\<span class=\"hljs-tag\">Commands<\/span>\\<span class=\"hljs-tag\">CustomCommands<\/span>\r\n\u00a0 \u00a0 <span class=\"hljs-tag\">tags<\/span>:\r\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-tag\">-<\/span> <span class=\"hljs-rules\">{ <span class=\"hljs-rule\"><span class=\"hljs-attribute\">name<\/span>:<span class=\"hljs-value\"> drush.command <\/span><\/span><\/span>}<\/code><\/pre>\n<p><strong>3. CustomCommands.php file.<\/strong><\/p>\n<pre><code class=\"language-php hljs \"><span class=\"hljs-preprocessor\">&lt;?php<\/span>\r\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">Drupal<\/span>\\<span class=\"hljs-title\">drush_command_example<\/span>\\<span class=\"hljs-title\">Commands<\/span>;\r\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Drush<\/span>\\<span class=\"hljs-title\">Commands<\/span>\\<span class=\"hljs-title\">DrushCommands<\/span>;\r\n\r\n\r\n<span class=\"hljs-comment\">\/**\r\n\u00a0* Drush command file.\r\n\u00a0*\/<\/span>\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CustomCommands<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">DrushCommands<\/span> {<\/span>\r\n\r\n\u00a0 <span class=\"hljs-comment\">\/**\r\n\u00a0 \u00a0* A custom Drush command to displays the given text.\r\n\u00a0 \u00a0*\u00a0\r\n\u00a0 \u00a0*<span class=\"hljs-phpdoc\"> @command<\/span> drush-command-example:print-me\r\n\u00a0 \u00a0*<span class=\"hljs-phpdoc\"> @param<\/span> $text Argument with text to be printed\r\n\u00a0 \u00a0*<span class=\"hljs-phpdoc\"> @option<\/span> uppercase Uppercase the text\r\n\u00a0 \u00a0*<span class=\"hljs-phpdoc\"> @aliases<\/span> ttndrush,tthndrush-print-me\r\n\u00a0 \u00a0*\/<\/span>\r\n\u00a0 <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">printMe<\/span><span class=\"hljs-params\">(<span class=\"hljs-variable\">$text<\/span> = <span class=\"hljs-string\">'Hello world!'<\/span>, <span class=\"hljs-variable\">$options<\/span> = [<span class=\"hljs-string\">'uppercase'<\/span> =&gt; FALSE])<\/span> {<\/span>\r\n\u00a0 \u00a0 <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-variable\">$options<\/span>[<span class=\"hljs-string\">'uppercase'<\/span>]) {\r\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-variable\">$text<\/span> = strtoupper(<span class=\"hljs-variable\">$text<\/span>);\r\n\u00a0 \u00a0 }\r\n\u00a0 \u00a0 <span class=\"hljs-variable\">$this<\/span>-&gt;output()-&gt;writeln(<span class=\"hljs-variable\">$text<\/span>);\r\n\u00a0 }\r\n}<\/code> Enable module or if you created drush command in existing module than please clear cache. you can see custom drush command in list using<\/pre>\n<pre><code class=\"language-bash hljs \">drush list<\/code><\/pre>\n<pre>We can use our newly created drush command as below.\r\n\r\ndrush ttndrush --help \r\ndrush ttndrush <span class=\"hljs-string\">\"hello from to the new\"<\/span> \r\ndrush ttndrush <span class=\"hljs-string\">\"hello from to the new\"<\/span> --uppercase \r\ndrush ttndrush-<span class=\"hljs-keyword\">print<\/span>-me\u00a0<span class=\"hljs-string\">\"hello from to the new\"<\/span> \r\ndrush ttndrush-<span class=\"hljs-keyword\">print<\/span>-me <span class=\"hljs-string\">\"hello from to the new\"<\/span>\u00a0--uppercase<\/pre>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this tutorial se are going learn how to create custom Drush command in drupal. For that we need to create a custom module. The file structure will be like below drush_command_example \u00a0 \u00a0Commands \u00a0 \u00a0 \u00a0 CustomCommands.php \u00a0 drush_command_example.info \u00a0 drush.services.yml 1. drush_command_example.info file name: Drush Command Example description: This is our custom drush [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":430},"categories":[3602],"tags":[4862,5003],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55330"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55330"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55330\/revisions"}],"predecessor-version":[{"id":55332,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55330\/revisions\/55332"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}