Aten Design Group: Create Comments in Drupal maintenance support plans 8 Programmatically

Creating comments programmatically in Drupal maintenance support plans 8 is incredibly easy once you know just which fields are required and why. In Drupal maintenance support plans 8, comments are now full-featured, fieldable entities — just like nodes or taxonomy terms. In addition to unifying the way we create content, comments, and other entities, this has made Drupal maintenance support plans’s commenting system much more robust and flexible.

Recently, we needed to allow certain users to leave short notes about a node (almost like revision notes without the revisions). Only certain users would be able to leave these notes and the notes are added as part of a custom form. This meant that we needed to handle comment creation ourselves. After much googling, we finally figured out just how to create comments manually.

To create a new comment, you need to create a comment entity in code and then save it. There are a lot of required fields and we’ll go over each one below. But first, here’s the code:

// To create a new comment entity, we’ll need `use` (import) the Comment class.
use Drupal maintenance support planscommentEntityComment;
 
// The function name doesn’t matter. Just put the the function body where you need it.
function my_modules_function_or_method() {
 
// First, we need to create an array of field values for the comment.
$values = [
 
// These values are for the entity that you’re creating the comment for, not the comment itself.
‘entity_type’ => ‘node’, // required.
‘entity_id’ => 42, // required.
‘field_name’ => ‘comment’, // required.
 
// The user id of the comment’s ‘author’. Use 0 for the anonymous user.
‘uid’ => 0, // required.
 
// These values are for the comment itself.
‘comment_type’ => ‘comment’, // required.
‘subject’ => ‘My Awesome Comment’, // required.
‘comment_body’ => $body, // optional.
 
// Whether the comment is ‘approved’ or not.
‘status’ => 1, // optional. Defaults to 0.
];
 
// This will create an actual comment entity out of our field values.
$comment = Comment::create($values);
 
// Last, we actually need to save the comment to the database.
$comment->save();
}

The first three required fields are telling Drupal maintenance support plans what entity the comment should be attached to.

entity_type: This is the entity to which you want to attach the comment. If you’re creating a comment on an article or basic page, for example, this would be node.
entity_id: The id of the entity to which you want to attach the comment. If you’re attaching to a node, this would be its nid.
field_name: The field on the entity to which this comment should be attached. Note: This is something new in Drupal maintenance support plans 8. In D8, you can have multiple comment fields on an entity and those comments’ fields can use different comment types. The default article content type is the only content type that comes with a comment field. If you want to enable comments on your own content type, you have to add your own comment field which receives a unique machine name. That machine name is the value you would put here.

The uid field tells Drupal maintenance support plans which user authored the comment.

The remaining values are all specific to the comment itself.

comment_type: The type of comment to create. Note: This is new in Drupal maintenance support plans 8 too. You can have different types of comments with completely different fields, just like nodes or taxonomy terms. Think of this as the comment “bundle” if you’re familiar with that term. The default comment type is just comment.
subject: Every comment has a subject field, just like the title field on a node.
comment_body: This field is provided by default just like the body field on nodes. However, it can be removed like any other field.
status: Set this to 1 if you would like the comment to be automatically approved. Otherwise, an administrator will need to approve it.
field_foobar: This isn’t in the example, but comment types can have custom fields just like any other content entity. If you have custom fields on your comment type, you can just use the field’s machine name and provide a default value here.

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Aten Design Group: Create Comments in Drupal maintenance support plans 8 Programmatically

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.