How to write custom module in drupal 8/9

In order to write your own custom module in drupal 8/9, you need to create basically two files as below, if you would like to name your module to "dcodes_custom_rules_action":

dcodes_custom_rules_action.info.yml
dcodes_custom_rules_action.module

Here might be the code for file ".info.yml":

name: Dcodes Custom Rules Action
description: Custom Module By Dewan For Node Create Event
package: Dcodes
type: module
core: 8.x

Here might be the code for file ".module":

<?php

use Drupal\node\NodeInterface;
use \Drupal\node\Entity\Node;
use \Drupal\user\Entity\User;
use \Drupal\filter\Entity\FilterFormat;
function dcodes_custom_rules_action_node_insert(NodeInterface $node) {
if ($node->bundle() == 'your_content_type_machine_name_1' and $node->isPublished()) {
$pid_mail = $node->id();
/* email code start */
/* Prepare Proprty Email Data START */
if(isset($pid_mail)){
$land_node_obj = Node::load($pid_mail);
if (!is_null($land_node_obj)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();

//drupal_set_message(t('Test status messages here'), 'status');
$prop_nid = $land_node_obj->id();
$prop_title = $land_node_obj->getTitle(); // Title
$prop_author_uid = $land_node_obj->getOwnerId();
$prop_author_uid_obj = \Drupal\user\Entity\User::load($prop_author_uid);
if (!is_null($prop_author_uid)) {
$prop_author_name = $prop_author_uid_obj->getDisplayName(); // Author Name
$prop_author_f_name = $prop_author_uid_obj->field_first_name->value; // Author First Name
$prop_author_email = $prop_author_uid_obj->getEmail(); // Author Email
}
$prop_created_date_raw = $land_node_obj->getCreatedTime();
$prop_created_date = \Drupal::service('date.formatter')->format($prop_created_date_raw, 'm/d/Y h:i A'); // Created Date
$prop_url = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$prop_nid, $langcode); // URL

require_once getcwd()."/Mail-1.4.1/Mail-1.4.1/Mail.php";
$from = "contact@dewancodes.com";
$to = $prop_author_email; // REAL AUTHOR EMAIL

$cc = 'contact@dewancodes.com';
$bcc = 'contact@dewancodes.com';
$email_recipients = $to.", ".$cc.", ".$bcc;
$subject = "A new NODENAME has been registered";
if($langcode=='en'){
$body =
'

Hello '.$prop_author_f_name.',

A new NODENAME has been added to your account

'.$prop_title.'

Need help? We are here for your service.
Learn more about how you can contact us.

Dewan Codes Team
www.dewancodes.com

'
;
}

$host = "yourmailhost";
$username = "yourmailhostname";
$password = "yourmailhostpassword";

$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => '1.0',
'Content-type' => 'text/html; charset=iso-8859-1',
'Cc' => $cc,
'bcc' => $bcc
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($email_recipients, $headers, $body);

if (PEAR::isError($mail)) {
//echo("" . $mail->getMessage() . "");
} else {
//echo("New YourGoal Creation Message Successfully Sent To: ".$to."");
}
}
}
/* YourGoal CREATED end here END */
/* email code end */
}
// CODEOBJECTIVE CREATION START
if ($node->bundle() == 'your_content_type_machine_name_2' and $node->isPublished()) {
$pid_mail = $node->id(); // CODEOBJECTIVE NID
/* email code start */
/* Prepare CODEOBJECTIVE CONFIRMATION Email Data START */
if(isset($pid_mail)){
$land_node_obj = Node::load($pid_mail);
if (!is_null($land_node_obj)) {
$r_m_data_res_num = $node->id(); // // (1) CODEOBJECTIVE #
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
//drupal_set_message(t('Test status messages here'), 'status');
$prop_nid = $land_node_obj->id();
$prop_title = $land_node_obj->getTitle(); // Title
$prop_author_uid = $land_node_obj->getOwnerId();
$prop_author_uid_obj = \Drupal\user\Entity\User::load($prop_author_uid);
if (!is_null($prop_author_uid)) {
$prop_author_name = $prop_author_uid_obj->getDisplayName(); // CODEOBJECTIVE Creator (Guest) Name
$prop_author_f_name = $prop_author_uid_obj->field_first_name->value; // (2) CODEOBJECTIVE Creator (Guest) First Name
$prop_author_email = $prop_author_uid_obj->getEmail(); // (3) CODEOBJECTIVE Creator (Guest) Email
}
$prop_created_date_raw = $land_node_obj->getCreatedTime();
$prop_created_date = \Drupal::service('date.formatter')->format($prop_created_date_raw, 'm/d/Y h:i A'); // Created Date
$prop_url = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$prop_nid, $langcode); // URL
// CCK fields START , CODEOBJECTIVE CREATE, CODEOBJECTIVE CONTENT TYPE $land_node_obj
foreach ($land_node_obj->getFields() as $name => $field) {
$entity_reference_fields_arr = array(
'field_fixing_plot'
);
if(in_array($name,$entity_reference_fields_arr)){
$this_ref_fld_ids_data_arr = $land_node_obj->get($name)->referencedEntities();
foreach($this_ref_fld_ids_data_arr as $entity) {
$r_m_data_plot_nid = $entity->ID(); // plot NID
$r_m_data_plot_name = $entity->label(); // (4) plot Name
// plot DATA in YourGoal fixing, START
if(isset($r_m_data_plot_nid)){
$plot_node_obj = Node::load($r_m_data_plot_nid);
if (!is_null($plot_node_obj)) {
$r_m_data_plot_author_uid = $plot_node_obj->getOwnerId(); // plot AUTHOR (CODEOBJECTIVE HOST) UID
$r_m_data_plot_author_uid_obj = \Drupal\user\Entity\User::load($r_m_data_plot_author_uid);
if (!is_null($r_m_data_plot_author_uid_obj)) {
$r_m_data_plot_author_name = $r_m_data_plot_author_uid_obj->getDisplayName(); // plot AUTHOR (CODEOBJECTIVE HOST) name
$r_m_data_plot_author_f_name = $r_m_data_plot_author_uid_obj->field_first_name->value; // (5) plot AUTHOR (CODEOBJECTIVE HOST) First Name
$r_m_data_plot_author_email = $r_m_data_plot_author_uid_obj->getEmail(); // (6) plot AUTHOR (CODEOBJECTIVE HOST) Email
}
// GET CODEOBJECTIVE ADDRESS START
$r_m_data_plot_land_id_arr = $plot_node_obj->get('field_plots_properties')->referencedEntities();
foreach($r_m_data_plot_land_id_arr as $entity_spc_prop) {
$r_m_data_plot_land_id = $entity_spc_prop->ID(); // plot YourGoal NID
if(isset($r_m_data_plot_land_id)){
$r_m_data_plot_land_id_obj = Node::load($r_m_data_plot_land_id);
$r_m_data_plot_land_name = $r_m_data_plot_land_id_obj->getTitle(); // (9) CODEOBJECTIVE YourGoal Name
// GET CODEOBJECTIVE YourGoal Owner Details START
$r_m_data_plot_land_author_uid = $r_m_data_plot_land_id_obj->getOwnerId(); // YourGoal AUTHOR (OWNER) UID
$r_m_data_plot_land_author_uid_obj = \Drupal\user\Entity\User::load($r_m_data_plot_land_author_uid);
if (!is_null($r_m_data_plot_land_author_uid_obj)) {
$r_m_data_plot_land_author_name = $r_m_data_plot_land_author_uid_obj->getDisplayName(); // YourGoal AUTHOR name
$r_m_data_plot_land_author_f_name = $r_m_data_plot_land_author_uid_obj->field_first_name->value; // (10) YourGoal AUTHOR First Name
$r_m_data_plot_land_author_email = $r_m_data_plot_land_author_uid_obj->getEmail(); // (11) YourGoal AUTHOR Email
}
// GET CODEOBJECTIVE YourGoal Owner Details START

// Address START
if (isset($r_m_data_plot_land_id_obj->field_land_address)) {
if (count($r_m_data_plot_land_id_obj->field_land_address->getValue()) > 0) {
$address['country_code'] = $r_m_data_plot_land_id_obj->field_land_address->getValue()[0]['country_code'];
if (\Drupal::service('country_manager')->getList()[$address['country_code']]) {
$address['country_name'] = \Drupal::service('country_manager')->getList()[$address['country_code']]->__toString();
}
$address['administrative_area'] = $r_m_data_plot_land_id_obj->field_land_address->getValue()[0]['administrative_area'];
$address['locality'] = $r_m_data_plot_land_id_obj->field_land_address->getValue()[0]['locality'];
$address['address_line1'] = $r_m_data_plot_land_id_obj->field_land_address->getValue()[0]['address_line1'];

}
}
$r_m_data_plot_land_address = $address['address_line1'].', '.$address['locality'].', '.$address['country_name']; //$r_m_data_plot_land_id_obj->get('field_land_address')->getString(); // (7) CODEOBJECTIVE ADDRESS
// Address END

}

}
// GET CODEOBJECTIVE ADDRESS START
}
}
// plot DATA in YourGoal fixing, END

}
}
else if($name=='field_fixing_time'){ //
$r_m_data_fixing_time_date_start = "";
$r_m_data_fixing_time_date_end = "";
// GET CODEOBJECTIVE TIME START
if(isset($land_node_obj->field_fixing_time)){
$fixing_time_tid = $land_node_obj->field_fixing_time->getValue()[0]['target_id'];
$arr_times = get_paragraph_times_in_date_and_time_with_format($fixing_time_tid);
$str_date = $arr_times['start_date'];
$end_date = $arr_times['end_date'];
$str_start_time = $arr_times['start_time'];
$str_end_time = $arr_times['end_time'];
$r_m_data_fixing_time_date_start = $str_date.", " . $str_start_time . "-" . $str_end_time; // (8) CODEOBJECTIVE TIME
$r_m_data_fixing_time_date_end = $end_date.", " . $str_start_time . "-" . $str_end_time; // (8) CODEOBJECTIVE TIME
}
// GET CODEOBJECTIVE TIME END
}
}
// CCK fields END , CODEOBJECTIVE CREATE, CODEOBJECTIVE CONTENT TYPE

require_once getcwd()."/Mail-1.4.1/Mail-1.4.1/Mail.php";
$from = "contact@dewancodes.com";

$to = $prop_author_email;

$cc = $r_m_data_plot_author_email;
$bcc = 'contact@dewancodes.com';

$email_recipients = $to.", ".$cc.", ".$bcc;

if($langcode=='en'){
$subject = "NODE # ".$r_m_data_res_num." has been registered.";
$body =
'

Hello '.$prop_author_f_name.',

NODENUMBER # '.$r_m_data_res_num.' has been registered.

CODEOBJECTIVE details:

Find code to see data to include in emails as you want to include.

Full CODEOBJECTIVE details is available at http://www.dewancodes.com/contact-drupal-developer

Have a great experience,

Dewan Codes Team
www.dewancodes.com

Contact us

'
;
}

$host = "yourmailhost";
$username = "yourmailhostname";
$password = "yourmailhostpassword";

$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => '1.0',
'Content-type' => 'text/html; charset=iso-8859-1',
'Cc' => $cc,
'bcc' => $bcc
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($email_recipients, $headers, $body); // UNCOMMENT TO MAKE EMAIL SENDING WORK

if (PEAR::isError($mail)) {
//echo("" . $mail->getMessage() . "");
} else {
//echo("New YourGoal Creation Message Successfully Sent To: ".$to."");
}
}
}
/* CODEOBJECTIVE CONFIRMATION(CREATED) end here END */
/* email code end */
}

}