{"id":47023,"date":"2017-03-15T11:13:44","date_gmt":"2017-03-15T05:43:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=47023"},"modified":"2017-03-15T11:13:44","modified_gmt":"2017-03-15T05:43:44","slug":"how-to-programmatically-replace-the-content-in-the-custom-block-in-drupal-8","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-programmatically-replace-the-content-in-the-custom-block-in-drupal-8\/","title":{"rendered":"How to Programmatically Replace the Content in the Custom Block in Drupal 8?"},"content":{"rendered":"<p>Replacing the content in the custom block is often challenging.\u00a0In <a title=\"Drupal 8 Development Services\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\">Drupal 8<\/a>, the best way to replace the content in the custom block programmatically is to use module_preprocess_block alter.<\/p>\n<p>We have created a custom block and now we want to replace the value of the placeholder programmatically. For eg. On http:\/\/example.com\/, we have created a custom block named &#8220;Test Block&#8221; for the home page. The body of the\u00a0custom block contains the following code:<\/p>\n<p>[java]<\/p>\n<p>&lt;div&gt;<br \/>\n&lt;div class=&quot;\u201ccontent\u201d&quot;&gt;<br \/>\n&lt;h5&gt;&lt;a href=&quot;\u201c{{link}}\u201d&quot;&gt;Test Block&lt;\/a&gt;&lt;\/h5&gt;  \/\/ {{link}} represents the placeholder<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>[\/java]<\/p>\n<p>Now we want to replace the value of placeholder ({{link}}) with a node page URL link programmatically. Here&#8217;s how to go about it:<\/p>\n<p>Create an alter module by following the below steps:<\/p>\n<p>Step 1- Create a test folder in the modules folder<br \/>\nStep 2- Create &#8220;test.info.yml&#8221; file that will have entire information about module such as name, type, package, version.<\/p>\n<p>[java]<br \/>\nname: Test Block<br \/>\ndescription: &#8216;Module to replace the placeholder.&#8217;<br \/>\npackage: Custom<br \/>\ntype: module<br \/>\ncore: 8.x<br \/>\n[\/java]<\/p>\n<p>Step 3- Create &#8220;test.routing.yml&#8221; file and add routing for this module<br \/>\nStep 4- Create &#8220;test.module&#8221; file which will contain the preprocess alter of the custom block. The custom block contains the placeholder<br \/>\nStep 5- Open the test.module file and add preprocess alter code as shown below:<\/p>\n<p>[java]<br \/>\n use Drupal\\Core\\Block\\BlockBase;<br \/>\n use Drupal\\node\\Entity\\Node;<br \/>\n use Drupal\\Core\\Url;<br \/>\n use Drupal\\file\\Entity\\File;<br \/>\n use Drupal\\field\\Entity\\FieldConfig;<\/p>\n<p> \/**<br \/>\n * function preprocess block for Test block<br \/>\n *\/<br \/>\n function test_preprocess_block(&amp;$variables)<br \/>\n {<\/p>\n<p> if (isset($variables[&#8216;content&#8217;][&#8216;#block_content&#8217;]))<\/p>\n<p> {<br \/>\n $content = $variables[&#8216;content&#8217;][&#8216;#block_content&#8217;];<br \/>\n if( method_exists($content,&#8217;id&#8217;) &amp;&amp; $content-&gt;id() == &#8216;block_id&#8217;)<br \/>\n\/*<br \/>\n* Block Id represents id of coressponding block which contains the placeholder.<br \/>\n*\/<br \/>\n {<br \/>\n $body = $variables[&#8216;content&#8217;][&#8216;body&#8217;][0][&#8216;#text&#8217;];<br \/>\n \/*<br \/>\n * now you need to replace with placeholder by finding the dynamic link<br \/>\n *\/<br \/>\n $query = \\Drupal::entityQuery(&#8216;node&#8217;);<br \/>\n $query-&gt;range(0, 1);<br \/>\n $result = $query-&gt;execute();<br \/>\n $nids = array_values($result);<br \/>\n if (isset($nids[0]))<br \/>\n {<br \/>\n $node_load = Node::load($nids[0]);<br \/>\n $options = array(&#8216;absolute&#8217;=&gt;TRUE);<br \/>\n $url = Url::fromRoute(&#8216;entity.node.canonical&#8217;, [ &#8216;node&#8217;=&gt;$nids[0] ], $options);<br \/>\n $Url = $url-&gt;toString();<br \/>\n $body=str_replace(&#8216;{{link}}&#8217;,$Url,$body);<br \/>\n $variables[&#8216;content&#8217;][&#8216;body&#8217;][0][&#8216;#text&#8217;] = $body;<br \/>\n }<br \/>\n }<br \/>\n }<br \/>\n }<br \/>\n[\/java]<\/p>\n<p>The Block Id and the placeholder are highlighted in the following image:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-47029\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/03\/screenshot3-1024x575.png\" alt=\"screenshot(3)\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/03\/screenshot3-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/03\/screenshot3-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/03\/screenshot3-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/03\/screenshot3.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>We hope you will able to now replace the content in your custom block easily following the above steps. In case if you have more queries about Drupal 8 custom blocks, feel free to share them in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Replacing the content in the custom block is often challenging.\u00a0In Drupal 8, the best way to replace the content in the custom block programmatically is to use module_preprocess_block alter. We have created a custom block and now we want to replace the value of the placeholder programmatically. For eg. On http:\/\/example.com\/, we have created a [&hellip;]<\/p>\n","protected":false},"author":1077,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":148},"categories":[3602,1],"tags":[4496,4490,3601,4495,4489],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/47023"}],"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\/1077"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=47023"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/47023\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=47023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=47023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=47023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}