drupal 8

As we know drupal 8 use symphony, so module structure of drupal 8 is change and workflow functionality of doing thing also change. we can make Drupal 7 module procedure oriented but in drupal 8 module needs to follow object oriented MVC architecture.

In this tutorail we learn how can we create the node programmatically in drupal 8 with image upload using custom module.

Let say module name is "nodecreator"

1) To Create a module we need a nodecreator.info.yml file

name: Node Creater
type: module
description: Node created programmatically
package: Custom
core: 8.x

2) Now you can enable the module at "/admin/modules" path on your website.

3) Create a routing file nodecreator.routing.yml for menu path to call a controller function

nodecreator:
    path: /add/nodecreate
    defaults:
        _controller: Drupal\nodecreator\Controller\PostController::newnode
    requirements:
        _permission: 'access content'

4) Create a controller file in module folder using this folder structure - "src/Controller/PostController.php" and add following content

<?php

namespace Drupal\nodecreator\Controller;

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

class PostController {
	public function newnode() {
		$data = file_get_contents("path/data-folder/image.png");
		$file = file_save_data($data, "public://image.png", FILE_EXISTS_REPLACE);
		 
		// Create node object and save it.
		$node = Node::create([
		  'type'=> 'article',
		  'title'=> 'Node By Email',
		  'body'=> 'This is just a content',
          'field_image'=> [
              'target_id' => $file->id(),
              'alt' => 'Sample',
              'title' => 'nodecreate Image'
          ],
		]);
		$node->save();

		return array(
                '#title' => 'Node Creator',
                '#markup' => 'Node Created Successfully.',
            );
    }
 
}

5) Now open the path "/add/nodecreate" and your node will we created.

 

Note : That example is use for creating articles content with body and field_image field, if you not have that field or having different name, please change it else you will get error.


Please Comment your thoughts and feedback below and add something if you found good in anywhere to help others

Hit a like Button If you like the Post.

Many Thanks

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Other Articles

Wordpress Error with php 7.1 - PHP Fatal error: Uncaught Error: [] operator not supported for strings

Hey, This is error caused by php7 and later,many application will face this error Reason behind is that the php 7 will not convert the blank variable with is define as string to array:

How to Install Civicrm into Drupal 7

There is a direct guide available on CiviCRM Website, Here we follow faster steps to do the same.

Drupal 8 correct a word by default browser auto correct options on textarea field causing by CKEditor

If you are facing the problem to to correct the word in drupal 8 text because of copy and paste default popup on right, we have a simple trick to get over it 

check if a string contains a specific word with PHP

In PHP sometimes we need to find the specific word in the string for the programitically use the code, there is a function called "

Export Mysql database using SSH

Export The database directly from command line is Easy and Fast. Follow these steps to export the database using SSH (putty terminal)

How to create the directories and sub directories in one command linux

Many times we are want to create multiple directories and sub-directories under it and usually we do cd and mkdir one by one, it is very time consuming and it sometimes slow down our workflow.

Install Multiple PHP 7.3 / 7.2 / 7.1 on Ubuntu 18.04 / Ubuntu 16.04

If you want to Install Multiple PHP version on ubuntu/Linux system then this tutorial is for you. and on ubuntu shell using root user and follows steps below:

Update Drupal 8 core manually

This is small tutorial to how to update the Drupal 8 core manually using shell or FTP, Let start -

Check the size of folder, files and directories using SSH

Following commands(In Bold) used for different purpose  1. Use df -h For check out the used space, free space and total space From all the partitions.

Drupal 7 content type template

Open the template.php file in your theme for editing. Look for a function called yourthemename_preprocess_page (replace the yourthemename with your theme’s name).

How to override different version of Jquery in Drupal 8 using theme

Drupal 8 use its own jquery version by default and if you want to use the other version of jQuery instead of default than this tutorials steps is for you, sometimes we need different version to oth

Render a drupal node d7

If you want to render a node in drupal 7 it is pretty easy, you just a node id or nid of node for doing this: simply

Send Email using smtp module with Gmail

Here is the link that helps to configure stmp module with Gmail configuration and enables drupal

Recursively delete all files of a specific extension

Usually we all use "rm" command to delete or remove the files and folder on linux, but if we want to delete the specific type of content/files then we can use "find" command to find and "delete" co

Drush Installation

Heys, Steps to installing drush using composer on server - login to putty - check the composer is install and working by "composer --version"

Subtract two angularjs dates

hey, Here you get how to subtract to date in angularjs with core functionality

Bootstrap subtheme dropdown Drupal 7

Steps to create sub theme and mutilevel dropdown :-

List of all countries

Here is the GitHub Link that helps me to get the list of countries at once May it helps to you also 

Menu template in drupal 8 for specific customization

Drupal 8 gives the flexibility to make menu template and design the menu with our need, to do this --

Flexslider Arrows Changed by arrow Images

Flexslider uses the arrows of its own flexslider family icons, if you want to change or replace with yours images you can do using following css.  

Drupal 7 Multisite setup

Here is some links will helps to create a multisite of drupal 7 

Render block and views block drupal 7 programmatically

This is article is for Drupal 7 to render block in templates and custom blocks using php codes Actually there is very simple ways to render block pro-grammatically 

Wordpress Revolution Slider error after upgrade to php 7

A recently got the error below after updating wordpress website to php 7, error causing the website blank and when the debug mode on we got this error, caused by revolution slider.

Recent Posts

Create a Flag link programmatically and render flag link in twig template drupal 8

This article is refer to generate the flag link programmatically and render in drupal 8 twig template, before in drupal 7 its was quiet easy to render or generate flag link in just a single line of

Jquery prevent enter to submit form

If you want to prevent the form submit using jquery, then this page helps you to doing this.

Code:

Play Pause HTML5 Video using Jquery

HTML5 video playing on any web page is very easy, in this tutorial we can show you how to add video on html and make it play and pause without using html controls.

Import and Export Drupal 8 Views, Or Import and Export any configuration Drupal 8

Import and Export configuration is the basic protocol to staging the website or production website deployment, In Drupal 7, we need the extra modules to import and export or we can use features mod

How to override different version of Jquery in Drupal 8 using theme

Drupal 8 use its own jquery version by default and if you want to use the other version of jQuery instead of default than this tutorials steps is for you, sometimes we need different version to oth

Subscribe to Mailchimp programmatically via api with PHP

In this tutorial we can see how to subscribe to the mailchimp subscribe list using php programmatically via api,

For this you need a mailchimp account, api key and list id 

How you can get Mailchimp API KEY

By Follow steps below, you can find the api key or create the api key at mailchimp and use it to your website to connected the site with mailchimp to collect the subscribe in fastest way.

How to get your MailChimp LIST ID

Mailchimp is marketing automation platform and is provide email for marketing your product or business whatever you like, today its most trusted service providing all over the globe.

Drupal 8 add custom menu classes to using menu.html.twig

In Drupal 8 Twig templates give extra power to drupal to add dynamic content in twig style, In this tutorial we can see how we can add the custom menu classes to menu ul li using menu.html.twig.

How to create the directories and sub directories in one command linux

Many times we are want to create multiple directories and sub-directories under it and usually we do cd and mkdir one by one, it is very time consuming and it sometimes slow down our workflow.

Uncaught TypeError: CKEDITOR.tools.object.keys is not a function

Intergation of code snippet with ckeditor, cause this error "Uncaught TypeError: CKEDITOR.tools.object.keys is not a function".