<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by B S Pavan on Medium]]></title>
        <description><![CDATA[Stories by B S Pavan on Medium]]></description>
        <link>https://medium.com/@bspavan?source=rss-b99672b5da8b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JGV7fNyVSbpoDHKKN1VvCA.jpeg</url>
            <title>Stories by B S Pavan on Medium</title>
            <link>https://medium.com/@bspavan?source=rss-b99672b5da8b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:58:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bspavan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Object-Oriented Programming Concepts in PHP — Part 1]]></title>
            <link>https://medium.com/valuebound/object-oriented-programming-concepts-in-php-part-1-9332c230f1f2?source=rss-b99672b5da8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9332c230f1f2</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[drupal]]></category>
            <dc:creator><![CDATA[B S Pavan]]></dc:creator>
            <pubDate>Sat, 22 Apr 2017 21:47:10 GMT</pubDate>
            <atom:updated>2017-04-22T21:47:10.996Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uAs6fT1lkSRJZ0-IluwABA.png" /><figcaption>Object-Oriented Programming Concepts in PHP — 1</figcaption></figure><p>PHP is a server-side scripting language, mainly used for web development but also used as a general-purpose programming language. Object-Oriented Programming (PHP OOP), is a type of programming language principle added to php5, that helps in building complex, reusable web applications.</p><p>In this blog, we will be explaining some of Object-Oriented Programming concepts in PHP with some examples.</p><p>The Object Oriented concepts in PHP are :</p><ul><li>Class</li><li>Objects</li><li>Inheritance</li><li>Interface</li><li>Abstraction</li><li>Magic Methods</li></ul><p><strong>Class &amp; Object:</strong></p><ul><li>Class is a programmer-defined data type, which includes local methods and local variables.</li><li>Class is a collection of objects. Object has properties and behaviour.</li><li>First we have to define a php class, where classname should be same as filename.</li></ul><p><strong>Example for simple class:</strong></p><pre>&lt;?php<br>class Books{<br>public function name(){<br>echo “Drupal book”;<br>}</pre><pre>public function price(){<br>echo “900 Rs/-”;<br>}<br>}</pre><pre>//To create php object we have to use a new operator. Here php object is the //object of the Books Class.</pre><pre>$obj = new Books();<br>$obj-&gt;name();<br>$obj-&gt;price();</pre><p><strong>Output:</strong></p><p><strong>Drupal book</strong></p><p><strong>900 Rs/-</strong></p><p>In the basics of object oriented ,let see how to define a class and create an object :</p><pre>&lt;?php</pre><pre>class Mobile {<br>/* Member variables */<br>var $price;<br>var $title;<br>/* Member functions */</pre><pre>function setPrice($par){<br>$this-&gt;price = $par;<br>}</pre><pre>function getPrice(){<br>echo $this-&gt;price .”&lt;br/&gt;”;<br>}</pre><pre>function setName($par){<br>$this-&gt;title = $par;<br>}</pre><pre>function getName(){<br>echo $this-&gt;title .” &lt;br/&gt;”;<br>}<br>}</pre><pre>?&gt;</pre><p>Note:</p><ol><li>In the above example name of the class is Mobile and the variable <strong>$this</strong> is a special variable and it refers to the same object</li></ol><p><strong>Creating Objects in PHP</strong></p><p>When class is created, we can create any number of objects to that class. Object is created with the help of new keyword.</p><pre>$Samsung = new Mobile();<br>$Xiaomi = new Mobile();<br>$Iphone = new Mobile();</pre><h3>Calling Member Function</h3><p>When the object is created we can access the variables and method function of the class with the help of operator ‘-&gt;, accesing the method is done to get the information of that method. Also look into how <a href="http://valuebound.com/resources/blog/til-%E2%80%93-access-object-properties-variable">we can access object properties via variables</a></p><pre>$Samsung-&gt;setName( “SamsungS8 );<br>$Iphone-&gt;setName( “Iphone7s” );<br>$Xiaomi-&gt;setName( “MI4” );<br>$Samsung-&gt;setPrice( 90000 );<br>$Iphone-&gt;setPrice( 65000 );<br>$Xiaomi-&gt;setPrice( 15000 );</pre><p>Now you call another member functions to get the values set by in above example</p><pre>$Samsung-&gt;getName();<br>$Iphone-&gt;getName();<br>$Xiaomi-&gt;getName();<br>$Samsung-&gt;getPrice();<br>$Iphone-&gt;getPrice();<br>$Xiaomi-&gt;getPrice();</pre><p><strong>Output for the above code</strong></p><p><strong><em>Samsung S8</em></strong></p><p><strong><em>Iphone S7</em></strong></p><p><strong><em>MI4</em></strong></p><p><strong><em>90000</em></strong></p><p><strong><em>65000</em></strong></p><p><strong><em>15000</em></strong></p><p><strong>Inheritance</strong></p><p>When the properties and the methods of the parent class is accessed by the child class , we call the concept has inheritance. The child class can inherit the parent method and give own method implementation , this property is called overridden method. When the same method of the parent class is inherited we call as inherited method.</p><p>Now let us see types of inheritance supported in Object Oriented Programming and corresponding Php inheritance examples.</p><p>Types Of Inheritance</p><ol><li>Single Level Inheritance</li><li>Multilevel Inheritance</li></ol><p><strong>Single Level Inheritance:</strong> In Single Level Inheritance the Parent class methods will be extended by the child class. All the methods can be inherited.</p><p><strong>Single Level Inheritance</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*9D_yUEtwwuvEQFlZ." /><figcaption>Single Level Inheritance</figcaption></figure><p><strong>Example for Single Level Inheritance</strong></p><pre>&lt;?php<br>class A<br>{<br>public function printItem($string)<br>{<br>echo ‘ Hi : ‘ . $string;<br>}<br>public function printPHP()<br>{<br>echo ‘I am from valuebound’ . PHP_EOL;<br>}<br>}</pre><pre>class B extends A<br>{<br>public function printItem($string)<br>{<br>echo ‘Hi: ‘ . $string . PHP_EOL;<br>}</pre><pre>public function printPHP()<br>{<br>echo “I am from ABC”;<br>}<br>}</pre><pre>$a = new A();<br>$b = new B();<br>$a-&gt;printItem(‘Raju’);<br>$a-&gt;printPHP();<br>$b-&gt;printItem(‘savan’);<br>$b-&gt;printPHP();</pre><pre>?&gt;</pre><p><strong>Output</strong></p><p><strong>Hi : Raju</strong></p><p><strong>I am from valuebound</strong></p><p><strong>Hi: savan</strong></p><p><strong>I am from ABC</strong></p><p><strong>MultiLevel Inheritance</strong> : In MultiLevel Inheritance the parent class method will be inherited by child class and again subclass will inherit the child class method.</p><p><strong>MultiLevel Inheritance</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*WfUOYzMSHFqCB3al." /></figure><p><strong>Example for MultiLevel Inheritance</strong></p><pre>class A<br>{<br>public function myage()<br>{<br>return ‘ age is 80’;<br>}<br>}</pre><pre>class B extends A<br>{<br>public function mysonage()<br>{<br>return ‘ age is 50’;<br>}<br>}</pre><pre>class C extends B<br> {<br>  public function mygrandsonage()<br>  {<br>   return ‘age is 20’;<br>   }<br>  public function myHistory() {<br>  echo “Class A “ .$this-&gt;myage();<br>  echo “Class B “.$this-&gt; mysonage();<br>  echo “ Class C “ . $this-&gt;mygrandsonage();<br>}<br>}</pre><pre>$obj = new C();<br>$obj-&gt;myHistory();</pre><p><strong>Output</strong></p><p><strong>Class A is 80</strong></p><p><strong>Class B is 50</strong></p><p><strong>Class C 20</strong></p><p><strong>INTERFACES:</strong></p><ul><li>An interface is a description of the actions that an object can do.</li><li>Interface is written in the same way as the class the declaration with interface keyword.</li></ul><p><strong>Rules of Interfaces:</strong></p><ul><li>All methods declared in an <strong>interface must be public</strong>; this is the nature of an interface.</li><li>All methods in the <strong>interface must be implemented within a class</strong>; failure to do so will result in a fatal error.</li><li>The class implementing the interface must use the <strong>exact same method signatures as are defined in the interface</strong></li><li>Interfaces can be extended like classes using the extends operator.</li></ul><p><strong>Example for the interface class</strong></p><pre>&lt;?php<br>interface A {<br>public function setProperty($x);<br>public function description();<br>}</pre><pre>class Mangoes implements A {<br>public function setProperty($x) {<br>$this-&gt;x = $x;<br>}</pre><pre>public function description() {<br>echo ‘Describing’ . $this-&gt;x . tree;<br>}<br>}</pre><pre>$Mango = new Mangoes();<br>$Mango-&gt;setProperty(mango);<br>$Mango-&gt;description();</pre><p><strong>Output:</strong></p><p><strong>Describing Mango tree</strong></p><p>2) Interface can be <strong>extended</strong> with another interface using extends keyword</p><pre>&lt;?php<br>interface A {<br>public function Compute();<br>}<br>interface B extends A {<br>public function Divide();<br>}<br>class C implements B {<br>public function Divide() {<br>$var=10;<br>$var1=2;<br>$var3=$var/$var1;<br>echo “division of 10/2 is” . $var3;<br>}</pre><pre>public function Compute() {<br>$a=2;<br>$b=3;<br>$c=$a*$b;<br>echo “multiplication of 2*3 is” . $c;<br>}<br>}</pre><pre>$obj = new C();<br>$obj-&gt;Divide();<br>$obj-&gt;Compute();</pre><pre>?&gt;</pre><p><strong>Output:</strong></p><p><strong>division of 10/2 is 5</strong></p><p><strong>multiplication of 2*3 is 6</strong></p><p><strong>Note on Interfaces:-</strong></p><ul><li>We cannot create objects to interface, but the class implementing the interface can have objects</li><li>We cannot define variable in interface.</li><li>If we extend interface all the methods of the interface must be implemented in child class.</li></ul><p><strong>Abstract Classes:</strong></p><p>An abstract class is a class that contains at least one abstract method. Abstract method is function declaration without anybody and it has only name of the method and its parameters.</p><p>There can be any number of methods in the class and we have to declare the class as abstract only when there is an abstract method</p><p>Example for Abstract class</p><pre>&lt;?php<br>abstract class Cars {<br>  public abstract function getCompanyName();<br>  public abstract function getPrice();<br>  }<br>class Baleno extends Cars {<br>  public function getCompanyName() {<br>  return “Maruthi Suzuki” . ‘&lt;br/&gt;’;<br>  }<br>public function getPrice() {<br>  return 720000 . ‘&lt;br/&gt;’;<br>  }<br>}<br>class Santro extends Cars {<br>  public function getCompanyName() {<br>  return “Hyundai” . ‘&lt;br/&gt;’;<br>  }<br>public function getPrice() {<br>  return 300000 . ‘&lt;br/&gt;’;<br>  }<br>}</pre><pre>$car = new Baleno();<br>$car1 = new Santro();<br>echo $car-&gt;getCompanyName();<br>echo $car-&gt;getPrice();<br>echo $car1-&gt;getCompanyName();<br>echo $car1-&gt;getPrice();<br>?&gt;</pre><p><strong>Output for the above code is:</strong></p><p><strong>Maruthi Suzuki</strong></p><p><strong>720000</strong></p><p><strong>Hyundai</strong></p><p><strong>300000</strong></p><p><strong>Notes on Abstract classes:</strong></p><ul><li>Objects cannot be created for the abstract classes.</li><li>If a class has only one method as abstract, then that class must be an abstract class.</li><li>The child class which extends an abstract class must define all the methods of the abstract class.</li><li>If the abstract method is defined as protected in the parent class, the function implementation must be defined as either protected or public, but not private.</li><li>The signatures of the methods must match, optional parameter given in the child class will not be accepted and error will be shown.</li><li>Abstract classes that declare all their methods as abstract are not interfaces with different names. One can implement multiple interfaces, but not extend multiple classes (or abstract classes).</li></ul><p>In the end , now we are able to create a class, define objects for the class and create methods. We also learnt on different topics of object oriented like inheritance, interface, abstraction. The basic concepts of OOP is explained in this blog.</p><p>Source: <a href="http://valuebound.com/resources/blog/object-oriented-programming-concepts-php-part-1">Object-Oriented Programming Concepts in PHP — Part 1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9332c230f1f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/valuebound/object-oriented-programming-concepts-in-php-part-1-9332c230f1f2">Object-Oriented Programming Concepts in PHP — Part 1</a> was originally published in <a href="https://medium.com/valuebound">Valuebound</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create custom Form with CRUD (Create, Delete, Update) operations in Drupal 8]]></title>
            <link>https://medium.com/valuebound/how-to-create-custom-form-with-crud-create-delete-update-operations-in-drupal-8-4437a0dc9b34?source=rss-b99672b5da8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4437a0dc9b34</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[drupal-development]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[drupal]]></category>
            <dc:creator><![CDATA[B S Pavan]]></dc:creator>
            <pubDate>Thu, 20 Apr 2017 17:12:55 GMT</pubDate>
            <atom:updated>2017-04-20T18:09:25.769Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZfBoCwM71VF1Xd2wOBSLSw.jpeg" /><figcaption>How to create custom Form with CRUD (Create, Delete, Update) operations in Drupal 8</figcaption></figure><p>Custom form with CRUD Operations is basically building the form with different fields like UID, Name, Mobile Number, Address, Email id etc. The CRUD operations for these fields is Deleting the value of the fields from the database or updating the existing value with new value , and printing the updated value.</p><p>How To Create Custom form with CRUD Operations.<br>To create a custom form with CRUD Operations, we need to follow the following folder structure as shown in the image. To perform the same operation of crud drupal we need to follow the following folder structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/0*fq2bTml9EeNjhH_m." /><figcaption>CRUD Folder Structure</figcaption></figure><p>The data from the database will be displayed in the form of table and the table contain operations as new column. The role of operations column is to perform edit(update) operation or the delete operation. The edit operation is used to update the present value into new values and delete operations is used to delete the entire row.<br>The following files and procedure helps us to create custom form with CRUD operations. Create the files as shown in the image.</p><p><strong>mydata.info.yml:</strong><br>The .info.yml files is most important part of Drupal Modules. The .info.yml file is a collection of static text file, used to define and configuring a module. First we have to create info.yml file which contains metadata about project like name of module , description, core, package.</p><pre><br>name: Form<br>description: A Form to enter user details.<br>core: 8.x<br>package: Custom<br>type: module</pre><p><strong>mydata.install:</strong><br>mydata.install helps to create a table in the database. I have created a table called mydata in the database.hook_schema helps to store the values into database. In our custom form the crud database will be stored in mydata as the table name. If the value is stored in the database, it helps in performing CRUD operations,like edit or delete. I have stored the fields as name, id, mobile number, email, age gender. Id is autoincrement with the help of serial type.</p><pre><br>&lt;?php<br>function mydata_schema() {<br> $schema[‘mydata’] = array(<br> ‘fields’ =&gt; array(<br> ‘id’=&gt;array(<br> ‘type’=&gt;’serial’,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘name’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 40,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘mobilenumber’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 40,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘email’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 40,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘age’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 25,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘gender’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 40,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ‘website’=&gt;array(<br> ‘type’ =&gt; ‘varchar’,<br> ‘length’ =&gt; 25,<br> ‘not null’ =&gt; TRUE,<br> ),<br> ),<br> ‘primary key’ =&gt; array(‘id’),<br> );<br> return $schema;<br>}</pre><p><strong>mydata.routing.yml</strong><br>mydata.routing.yml helps to transfer the function to the Controller to display of data in table format, to create form and placing that form in blocks. When ‘/mydata/hello/table’ is passed to the url the Controller DisplayTableController is called and the controller will display the data from database in table format. Simillarly when the ‘/mydata/form/mydata’ is passed to the url the form containing credentials like name, id , age etc is shown to the user. The user has to enter the details.</p><pre><br>mydata.mydata_controller_display:<br> path: ‘/mydata’<br> defaults:<br> _controller: ‘\Drupal\mydata\Controller\MydataController::display’<br> _title: ‘display’<br> requirements:<br> _permission: ‘access content’</pre><pre>mydata.mydata_form:<br> path: ‘/mydata/form/mydata’<br> defaults:<br> _form: ‘\Drupal\mydata\Form\MydataForm’<br> _title: ‘MydataForm’<br> requirements:<br> _access: ‘TRUE’</pre><pre>mydata.display_table_controller_display:<br> path: ‘/mydata/hello/table’<br> defaults:<br> _controller: ‘\Drupal\mydata\Controller\DisplayTableController::display’<br> _title: ‘display’<br> requirements:<br> _permission: ‘access content’</pre><pre>mydata.delete_form:<br> path: ‘/mydata/form/delete/{cid}’<br> defaults:<br> _form: ‘\Drupal\mydata\Form\DeleteForm’<br> _title: ‘DeleteForm’<br> requirements:<br> _access: ‘TRUE</pre><p>When the path ‘/mydata/form/delete/{cid}’ is passed to url DeleteForm.php is called. By this file you can delete the fields present in the database row by row.<br><strong>Controller</strong><br>In the src/Controller create a file called <br>DisplayTableController : In this file we will display the output in the table format. We have used the standard way of displaying the values of row in table format.<br>The code for displaying the value in table format. The $header_table must be defined, like what are the column values that need to be displayed in the table format.</p><pre><br> $form[‘table’] = [<br> ‘#type’ =&gt; ‘table’,<br> ‘#header’ =&gt; $header_table,<br> ‘#rows’ =&gt; $rows,<br> ‘#empty’ =&gt; t(‘No users found’),<br> ];</pre><p>We have to define the $header_table, first. The header table is the column name that must be used for printing in table. The code of header table is shown below</p><pre><br> $header_table = array(<br> ‘id’=&gt; t(‘SrNo’),<br> ‘name’ =&gt; t(‘Name’),<br> ‘mobilenumber’ =&gt; t(‘MobileNumber’),<br> //’email’=&gt;t(‘Email’),<br> ‘age’ =&gt; t(‘Age’),<br> ‘gender’ =&gt; t(‘Gender’),<br> //’website’ =&gt; t(‘Web site’),<br> ‘opt’ =&gt; t(‘operations’),<br> ‘opt1’ =&gt; t(‘operations’),<br> );</pre><p>The entire code of <strong>DisplayTableController.php</strong> is</p><pre><br>&lt;?php<br>namespace Drupal\mydata\Controller;<br>use Drupal\Core\Controller\ControllerBase;<br>use Drupal\Core\Database\Database;<br>use Drupal\Core\Url;</pre><pre>/**<br> * Class DisplayTableController.<br> *<br> * <a href="http://twitter.com/package">@package</a> Drupal\mydata\Controller<br> */<br>class DisplayTableController extends ControllerBase {</pre><pre>public function getContent() {<br> // First we’ll tell the user what’s going on. This content can be found<br> // in the twig template file: templates/description.html.twig.<br> // <a href="http://twitter.com/todo">@todo</a>: Set up links to create nodes and point to devel module.<br> $build = [<br> ‘description’ =&gt; [<br> ‘#theme’ =&gt; ‘mydata_description’,<br> ‘#description’ =&gt; ‘foo’,<br> ‘#attributes’ =&gt; [],<br> ],<br> ];<br> return $build;<br> }</pre><pre>/**<br> * Display.<br> *<br> * <a href="http://twitter.com/return">@return</a> string<br> * Return Hello string.<br> */<br> public function display() {<br> /**return [<br> ‘#type’ =&gt; ‘markup’,<br> ‘#markup’ =&gt; $this-&gt;t(‘Implement method: display with parameter(s): $name’),<br> ];*/<br> //create table header<br> $header_table = array(<br> ‘id’=&gt; t(‘SrNo’),<br> ‘name’ =&gt; t(‘Name’),<br> ‘mobilenumber’ =&gt; t(‘MobileNumber’),<br> //’email’=&gt;t(‘Email’),<br> ‘age’ =&gt; t(‘Age’),<br> ‘gender’ =&gt; t(‘Gender’),<br> //’website’ =&gt; t(‘Web site’),<br> ‘opt’ =&gt; t(‘operations’),<br> ‘opt1’ =&gt; t(‘operations’),<br> );</pre><pre>//select records from table<br> $query = \Drupal::database()-&gt;select(‘mydata’, ‘m’);<br> $query-&gt;fields(‘m’, [‘id’,’name’,’mobilenumber’,’email’,’age’,’gender’,’website’]);<br> $results = $query-&gt;execute()-&gt;fetchAll();<br> $rows=array();<br> foreach($results as $data){<br> $delete = Url::fromUserInput(‘/mydata/form/delete/’.$data-&gt;id);<br> $edit = Url::fromUserInput(‘/mydata/form/mydata?num=’.$data-&gt;id);</pre><pre>//print the data from table<br> $rows[] = array(<br> ‘id’ =&gt;$data-&gt;id,<br> ‘name’ =&gt; $data-&gt;name,<br> ‘mobilenumber’ =&gt; $data-&gt;mobilenumber,<br> //’email’ =&gt; $data-&gt;email,<br> ‘age’ =&gt; $data-&gt;age,<br> ‘gender’ =&gt; $data-&gt;gender,<br> //’website’ =&gt; $data-&gt;website,</pre><pre>\Drupal::l(‘Delete’, $delete),<br> \Drupal::l(‘Edit’, $edit),<br> );<br>}<br> //display data in site<br> $form[‘table’] = [<br> ‘#type’ =&gt; ‘table’,<br> ‘#header’ =&gt; $header_table,<br> ‘#rows’ =&gt; $rows,<br> ‘#empty’ =&gt; t(‘No users found’),<br> ];<br> return $form;<br>}</pre><p><strong>Form</strong><br>In the src create a folder called Form, In the form folder we will create a files which will help to create form fields like Textfield, checkbox, selectlist, number etc..<br><strong>MydataForm</strong>: In this file we will create the form fields for UID, Name, Mobile Number, Email, age etc. For more info on form api visit <a href="https://www.drupal.org/docs/8/api/form-api/introduction-to-form-api">https://www.drupal.org/docs/8/api/form-api/introduction-to-form-api</a> . The complete code for the Mydata form is shown below</p><pre><br>?php<br>namespace Drupal\mydata\Form;<br>use Drupal\Core\Form\FormBase;<br>use Drupal\Core\Form\FormStateInterface;<br>use Drupal\Core\Database\Database;<br>use Symfony\Component\HttpFoundation\RedirectResponse;</pre><pre>/**<br> * Class MydataForm.<br> *<br> * <a href="http://twitter.com/package">@package</a> Drupal\mydata\Form<br> */<br>class MydataForm extends FormBase {<br>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function getFormId() {<br> return ‘mydata_form’;<br> }</pre><pre>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function buildForm(array $form, FormStateInterface $form_state) {</pre><pre>$conn = Database::getConnection();<br> $record = array();<br> if (isset($_GET[‘num’])) {<br> $query = $conn-&gt;select(‘mydata’, ‘m’)<br> -&gt;condition(‘id’, $_GET[‘num’])<br> -&gt;fields(‘m’);<br> $record = $query-&gt;execute()-&gt;fetchAssoc();<br> }<br> $form[‘candidate_name’] = array(<br> ‘#type’ =&gt; ‘textfield’,<br> ‘#title’ =&gt; t(‘Candidate Name:’),<br> ‘#required’ =&gt; TRUE,<br> //’#default_values’ =&gt; array(array(‘id’)),<br> ‘#default_value’ =&gt; (isset($record[‘name’]) &amp;&amp; $_GET[‘num’]) ? $record[‘name’]:’’,<br> );<br> $form[‘mobile_number’] = array(<br> ‘#type’ =&gt; ‘textfield’,<br> ‘#title’ =&gt; t(‘Mobile Number:’),<br> ‘#default_value’ =&gt; (isset($record[‘mobilenumber’]) &amp;&amp; $_GET[‘num’]) ? $record[‘mobilenumber’]:’’,<br> );<br> $form[‘candidate_mail’] = array(<br> ‘#type’ =&gt; ‘email’,<br> ‘#title’ =&gt; t(‘Email ID:’),<br> ‘#required’ =&gt; TRUE,<br> ‘#default_value’ =&gt; (isset($record[‘email’]) &amp;&amp; $_GET[‘num’]) ? $record[‘email’]:’’,<br> );</pre><pre>$form[‘candidate_age’] = array (<br> ‘#type’ =&gt; ‘textfield’,<br> ‘#title’ =&gt; t(‘AGE’),<br> ‘#required’ =&gt; TRUE,<br> ‘#default_value’ =&gt; (isset($record[‘age’]) &amp;&amp; $_GET[‘num’]) ? $record[‘age’]:’’,<br> );</pre><pre>$form[‘candidate_gender’] = array (<br> ‘#type’ =&gt; ‘select’,<br> ‘#title’ =&gt; (‘Gender’),<br> ‘#options’ =&gt; array(<br> ‘Female’ =&gt; t(‘Female’),<br> ‘male’ =&gt; t(‘Male’),<br> ‘#default_value’ =&gt; (isset($record[‘gender’]) &amp;&amp; $_GET[‘num’]) ? $record[‘gender’]:’’,<br> ),<br> );<br> $form[‘web_site’] = array (<br> ‘#type’ =&gt; ‘textfield’,<br> ‘#title’ =&gt; t(‘web site’),<br> ‘#default_value’ =&gt; (isset($record[‘website’]) &amp;&amp; $_GET[‘num’]) ? $record[‘website’]:’’,<br> );<br> $form[‘submit’] = [<br> ‘#type’ =&gt; ‘submit’,<br> ‘#value’ =&gt; ‘save’,<br> //’#value’ =&gt; t(‘Submit’),<br> ];<br> return $form;<br> }<br> /**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function validateForm(array &amp;$form, FormStateInterface $form_state) {<br> $name = $form_state-&gt;getValue(‘candidate_name’);<br> if(preg_match(‘/[^A-Za-z]/’, $name)) {<br> $form_state-&gt;setErrorByName(‘candidate_name’, $this-&gt;t(‘your name must in characters without space’));<br> }<br> if (!intval($form_state-&gt;getValue(‘candidate_age’))) {<br> $form_state-&gt;setErrorByName(‘candidate_age’, $this-&gt;t(‘Age needs to be a number’));<br> }<br> /* $number = $form_state-&gt;getValue(‘candidate_age’);<br> if(!preg_match(‘/[^A-Za-z]/’, $number)) {<br> $form_state-&gt;setErrorByName(‘candidate_age’, $this-&gt;t(‘your age must in numbers’));<br> }*/<br> if (strlen($form_state-&gt;getValue(‘mobile_number’)) &lt; 10 ) {<br> $form_state-&gt;setErrorByName(‘mobile_number’, $this-&gt;t(‘your mobile number must in 10 digits’));<br> }<br> parent::validateForm($form, $form_state);<br> }<br> /**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function submitForm(array &amp;$form, FormStateInterface $form_state) {</pre><pre>$field=$form_state-&gt;getValues();<br> $name=$field[‘candidate_name’];<br> //echo “$name”;<br> $number=$field[‘mobile_number’];<br> $email=$field[‘candidate_mail’];<br> $age=$field[‘candidate_age’];<br> $gender=$field[‘candidate_gender’];<br> $website=$field[‘web_site’];<br> if (isset($_GET[‘num’])) {<br> $field = array(<br> ‘name’ =&gt; $name,<br> ‘mobilenumber’ =&gt; $number,<br> ‘email’ =&gt; $email,<br> ‘age’ =&gt; $age,<br> ‘gender’ =&gt; $gender,<br> ‘website’ =&gt; $website,<br> );<br> $query = \Drupal::database();<br> $query-&gt;update(‘mydata’)<br> -&gt;fields($field)<br> -&gt;condition(‘id’, $_GET[‘num’])<br> -&gt;execute();<br> drupal_set_message(“succesfully updated”);<br> $form_state-&gt;setRedirect(‘mydata.display_table_controller_display’);<br> }<br> else<br> {<br> $field = array(<br> ‘name’ =&gt; $name,<br> ‘mobilenumber’ =&gt; $number,<br> ‘email’ =&gt; $email,<br> ‘age’ =&gt; $age,<br> ‘gender’ =&gt; $gender,<br> ‘website’ =&gt; $website,<br> );<br> $query = \Drupal::database();<br> $query -&gt;insert(‘mydata’)<br> -&gt;fields($field)<br> -&gt;execute();<br> drupal_set_message(“succesfully saved”);<br> $response = new RedirectResponse(“/mydata/hello/table”);<br> $response-&gt;send();<br> }<br> }<br>}</pre><p>In the above file we have performed both Insert and update operations. If the user clicks the edit operations then it will call the update functions or it will be insert operation.We have used three methods to perform the operation<br><strong>Buildform</strong>: It is the method which is used to construct the form with different types like textfield, email,select,checkbox etc</p><p><strong>ValidateForm</strong> : In this method we provide validation conditions for the form, that has to be validated before the form values get submitted<br>Submit form : In this method the values will be submitted to the database so, it can be retrieved. <br> 2. <strong>DeleteForm.php </strong>: In this file we will perform delete operation. I have followed certain standards to delete operation, we can also user db_delete operation. The complete code for the delete operation is</p><pre><br>&lt;?php<br>namespace Drupal\mydata\Form;<br>use Drupal\Core\Form\FormBase;<br>use Drupal\Core\Form\FormStateInterface;<br>use Drupal\Core\Form\ConfirmFormBase;<br>use Drupal\Core\Url;<br>use Drupal\Core\Render\Element;<br>/**<br> * Class DeleteForm.<br> *<br> * <a href="http://twitter.com/package">@package</a> Drupal\mydata\Form<br> */<br>class DeleteForm extends ConfirmFormBase {<br>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function getFormId() {<br> return ‘delete_form’;<br> }<br> public $cid;<br> public function getQuestion() { <br> return t(‘Do you want to delete %cid?’, array(‘%cid’ =&gt; $this-&gt;cid));<br> }<br> public function getCancelUrl() {<br> return new Url(‘mydata.display_table_controller_display’);<br>}<br>public function getDescription() {<br> return t(‘Only do this if you are sure!’);<br> }<br> /**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function getConfirmText() {<br> return t(‘Delete it!’);<br> }</pre><pre>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function getCancelText() {<br> return t(‘Cancel’);<br> }</pre><pre>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {</pre><pre>$this-&gt;id = $cid;<br> return parent::buildForm($form, $form_state);<br> }</pre><pre>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function validateForm(array &amp;$form, FormStateInterface $form_state) {<br> parent::validateForm($form, $form_state);<br> }</pre><pre>/**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function submitForm(array &amp;$form, FormStateInterface $form_state) {<br> $query = \Drupal::database();<br> $query-&gt;delete(‘mydata’)<br> -&gt;condition(‘id’,$this-&gt;id)<br> -&gt;execute();<br> drupal_set_message(“succesfully deleted”);<br> $form_state-&gt;setRedirect(‘mydata.display_table_controller_display’);<br> }<br>}</pre><p>Note : cid is the current value form the url and cid is stored in this-&gt;id(current id).<br>Block:<br><strong> MydataBlock</strong>: This block helps to place the form fields anywhere in the UI. Placing the block can be done by going to admin/Structure/block and place the block in any of the regions. The code of MydataBlock is shown below.</p><pre><br>&lt;?php<br>namespace Drupal\mydata\Plugin\Block;<br>use Drupal\Core\Block\BlockBase;<br>/**<br> * Provides a ‘MydataBlock’ block.<br> *<br> * <a href="http://twitter.com/Block">@Block</a>(<br> * id = “mydata_block”,<br> * admin_label = <a href="http://twitter.com/Translation">@Translation</a>(“Mydata block”),<br> * )<br> */<br>class MydataBlock extends BlockBase {<br> /**<br> * {<a href="http://twitter.com/inheritdoc">@inheritdoc</a>}<br> */<br> public function build() {<br> ////$build = [];<br> //$build[‘mydata_block’][‘#markup’] = ‘Implement MydataBlock.’;</pre><pre>$form = \Drupal::formBuilder()-&gt;getForm(‘Drupal\mydata\Form\MydataForm’);</pre><pre>return $form;<br> }<br>}</pre><p>The output of the above module is shown below<br>Mydatablock</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/0*4h-dsLnwvyR1nb9D." /><figcaption>custom form</figcaption></figure><p>The above screenshot display all the form fields that are to be entered by the user .It is a block that is placed <br>CrudOperationPage(output page): It display the option for delete or edit</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/0*yBsDzshh3rWxLZQv." /><figcaption>CRUD output</figcaption></figure><p>Reference : <a href="https://github.com/pavanBS/drupal-module-crud">https://github.com/pavanBS/drupal-module-crud</a><br><a href="http://valuebound.com/resources/blog/how-to-create-custom-form-crud-create-delete-update-operations-drupal-8">Also Published at Valuebound.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4437a0dc9b34" width="1" height="1" alt=""><hr><p><a href="https://medium.com/valuebound/how-to-create-custom-form-with-crud-create-delete-update-operations-in-drupal-8-4437a0dc9b34">How to create custom Form with CRUD (Create, Delete, Update) operations in Drupal 8</a> was originally published in <a href="https://medium.com/valuebound">Valuebound</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>