Drupal 8 Examples
Content migration into a Drupal website using spreadsheet or CSV data can be surprisingly effective — especially (counterintuitively?) with large and complex datasets. I’ve written about data migration in Drupal 8 a couple of times, but new projects keep highlighting the diversity of data sources and content configurations, reminding me over and over again that data migration doesn’t really have a one-size-fits-all solution. In this post we’ll get you from zero (fresh Drupal 8 install) to a basic CSV data migration — including entity references and multiple field values — in about 15 or 20 minutes. You can download the accompanying Aten CSV Migrate example module here, and skip straight to the instructions if you'd like.
- Thats because Drupal 8 is already the better technologically and the faster way to build Drupal applications (compared to Drupal 7). Drupal 8 is built on top of Symfony so the good news are that as you learn how to create controller for example in Drupal 8 - the way you would do it in Symfony is quite the same and vice versa.
- Tags Drupal, examples, showcase By WPDonuts Editorial – WPDonuts is a blog that aims to help WordPress users improve their own sites, their businesses, and their general understanding of all things WordPress.
Drupal 8 Examples. Find examples of Drupal 8 code discussed in our blog articles here: Console Command Demo; Custom Hooks Demo; Event Subscriber Demo; Simple Page Demo; Maintained by the developers at x-team developer blog.
Drupal 8 Examples Class
Drupal 8 offers a handful of powerful and extensible migration modules in core. These modules lay the foundation for a wide variety of migration methods including several flavors of CSV importers like the point-and-click Entity Importer module for Drupal 8 developed by my colleague Travis Tomka. GUI powered migrations are perfect for minimally to moderately complex content, but can have a hard time crossing the finish line alone for complicated datasets.
When GUIs overwhelm: CSV or Spreadsheet content migrations
Earlier this year I began working with the National Science Foundation’s National Ecological Observatory Network (NEON) to migrate robust ecological data collected from more than eighty terrestrial and aquatic field sites across the United States into a Drupal 8 website. The bulk of the data was destined for a content type with more than 75 unique fields, and the sheer volume and complexity of the data was steering me away from point-and-click interfaces.
Luckily for me, the NEON team are old hands when it comes to spreadsheet manipulation. Their expertise and flexibility were applied to formatting a spreadsheet export of their data specifically configured for an import into Drupal. Then their spreadsheets were exported to CSV, a format simple to consume into a Drupal website with the right modules and configurations. With just a little back and forth, we were well on our way to a successful, complex data migration with minimal custom code.
The basics: Getting spreadsheet or CSV data in Drupal
A quick introduction to configuring a CSV import using the Migrate Source CSV module for Drupal 8 is warranted. Once the basics are clear, a more complex import won’t be so overwhelming. If your data is in spreadsheet format, you’ll want to export it to CSV. The example below assumes a comma delimiter, no encapsulation characters, and no escape characters. For more complex content you’ll define these attributes in the source portion of your migration yaml file. The following steps culminate in a working example CSV data migration module you can download and tinker with. Note that this first example doesn’t discuss the more complex sample below, which is also included in the download.
- Ensure that the core Migrate module is enabled
- Install and enable Migrate Source CSV, Migrate Tools, and Migrate Plus
- Create a new custom module (or just download my working example) which will include an info file, a migrate file, a source file, and an install file following the outline below
Note: For related topics to this tutorial that you may need to understand, you may be interested in tutorials about using dependency injection, which is needed to get the logger service into your code. Understand the Service Container and Get a Service from the Container are good places to start with that. Additionally the Entity API Hooks tutorial uses logging as an example when working with entities.
Developers familiar with Drupal 7 will also be familiar with watchdog(), an API function that allows you to create a log message that appears on the Reports page in the Drupal administrative UI. Implementing D7’s hook_watchdog allows module developers to customize the destination of these log messages. In Drupal 8, both functions are replaced by a PSR-3-compatible logging interface (change notice).
In this tutorial, you’ll learn how to create the D8 equivalent of D7’s watchdog() function: creating a log message that appears on the Reports administrative page. In a future tutorial, we’ll cover how to create your own logger, and use it in your custom module. However, I've left you some hints and pointers at the end of this tutorial if you care to dive in before then.
Before and after
There is a straight-up D8 procedural equivalent of D7’s watchdog function, as we learn from the change notice:
Drupal 7 watchdog()
Drupal 8 logger class
Learn from Examples
I downloaded the Examples for Developers module (version 8.x-1.x, but please consult the project page for the latest version) and looked for an example of this in action. I found a good one in the content_entity_example module within the Examples project.
After downloading Examples, open the project in your code editor or IDE of choice. (With Drupal 8’s object-oriented framework, I highly recommend using an IDE such as PhpStorm, simply because it makes navigating classes, methods, and their implementations so much easier. If you’re new to using an IDE, we have a nice set of PhpStorm IDE how-to videos from the creators, JetBrains, available on Drupalize.Me.)
Let’s take a look at the
ContactDeleteForm class in examples/content_entity_example.
In your code editor, navigate to:
If you have Examples downloaded to a Drupal 8 installation, that path is:
submitform() method, we see the following:
Let’s break this down.
In this example, the
submitForm method relies on the Drupal service container wrapper, a class which smoothes the transition from procedural to object-oriented code in the Drupal code base. The double colon means that we’re accessing a class from “elsewhere,” or outside of the class that it’s called from. It’s kind of like using an absolute path instead of relative one.
A helper method that quickly creates a copy of the logger service.
In core/lib/Drupal.php, we find:
So, from this, it looks like the logger method is way to kick-start the logger factory and get a copy of the logger to use in our code.
c. module name
The name of the module, in this case “content_entity_example.”
d. severity-level method
Drupal 8 Examples Sites
The name of the method in the
Drupal::logger service (in this case “alert”), which will correspond to a severity level, i.e. debug, info, notice, warning, error, critical, alert, emergency. These can be also found in the
PSRLogLoggerInterface in the file
e. log message
This can be a simple quoted PHP string, or a string that utilizes placeholders. In the latter case, pass in an associative array of specially formatted placeholder keys and values into the second parameter.
f. placeholders (array)
An array of placeholder keys and values. The key is the placeholder name with the appropriate prefix (see below for explanation), and the value is what should be used as a value. In this case, a method is called that supplies the value. Placeholders are provided so that values from context may be provided in the message. The logger will transform any placeholder messages to a safe format for output to HTML. For more information, see:
@variable — Use this style of placeholder for most use-cases. Special characters in the text will be converted to HTML entities.
%variable — Use this style of placeholder to pass text through drupal_placeholder() which will result in HTML escaped text, then wrapped with <em> tags.
Our Serum Bundle includes all of our previously released Serum presets. We hand-crafted each sound from scratch giving you a huge collection of inspiring production-ready presets.This Serum Bundle pack is nothing but inspiration. Omnisphere wavetables for serum. The trap-inspired Serum Presets in this collection can be used in any genre from Hip-hop and RnB to Lo-Fi and Deep House, providing you with the ultimate tools for your next track. This bundle includes over 500 presets with custom-made skins and bonus melody loops to be used alongside the presets in your own productions.
:variable — Use this style of placeholder when substituting the value of an href attribute. Values will be HTML escaped, and filtered for dangerous protocols.
Drupal 8 Contact Form
How to view log messages as administrator
Log messages created with the
Drupal::logger service can be viewed and filtered on the Reports page of the administrative UI. In the Administrative menu, go to Reports > Recent log messages. On this page is a list of recent log messages which you can filter by type and severity.
Why log messages?
Why would you want to use logging in your modules? You know how debug messages are really valuable during development? Well, when a site is in production and debug messages are no longer an option on a public site, log messages are a way to keep tabs on important things that are happening on your site—from notices about users signing in, to major errors that signal a problem that needs immediate attention. They can help you troubleshoot problems and find errors—both large and small—that need attention.
What should I be logging?
You can and should log anything that will help you keep tabs on what your users are doing, how the system is performing, and behind-the-scenes, automatic notifications, which can help you troubleshoot various errors. Things like:
Drupal 8 Examples Printable
- Actions performed by users
- Recoverable errors
- 'Recurly notification processed'
As a developer, visit your Reports page often to learn about notices and recoverable errors that can let you know about places in your code that need attention. They may not be bringing down the system, but they are having some kind of impact and may point to one or more bugs.
Exercise: Implement your own logger
Drupal 8 Examples Questions
In Drupal 7, you could implement
hook_watchdog in your module if you wanted to log messages to custom locations. In Drupal 8, hook_watchdog has been removed, and you now register your logger as a service in your module’s MODULENAME.services.yml, and then in your logger class, implement the PsrLogLoggerInterface and customize the logging destinations as you wish.
Drupal 8 Database Query
Several examples and variations of this can be found in core; for example, in contact, aggregator, and syslog core modules.
In a future tutorial, we’ll walk you through how to implement your own custom logger. For now, start using Drupal 8's logger to create log messages in your fresh new Drupal 8 modules and keep an eye on that Reports page!