Feed aggregator

Deeson: Using Git Flow on a Drupal Project

Planet Drupal -

Gitflow is a branching model for git and is used for projects where you might have multiple streams of work running concurrently. For this labs post I'll assume you are already proficent with git version control and want to be better. An FAQ is provided at the bottom of the post.

If you are using Gitflow on a project then you will have the following 2 main branches:

  1. master - always is in the same state as the production site
  2. develop - only ever contains stable code, ready for production

 

In the Gitflow model, a developer never commits directly into master.  Master must always be as production. 

Hotfix branch

If there is an emergency and something needs to be changed in production, but you have work pending in develop which cannot go out to live, you can make a new branch from master - such a branch is called a hotfix branch. It should be named with a prefix of hotfix/ so someone listing out the branches available can immediatley see it is a hotfix. The suffix of its name should be its release number (e.g. if the current release on production is 1.5.0 then the next hotfix release will be 1.5.1 and the name of the hotfix branch will be hotfix/1.5.1)

When the hotfix is finished then its branch is merged into both master and develop so this urgent fix is applied to both the main master environment and the latest code in develop. A new release tag of 1.5.1 can then be cut from master and released.

Feature branch

When a new piece of work which is not an emergency hotfix needs to be done, this should always be placed into a new branch called a Feature branch. Feature branches are always cut from develop and so have all the latest code ready for production in them to build ontop of.

If you are using a project management system such as Jira it is a good idea to tie feature branches to issues so that the conversation and details around what is being developed in git can be connected together. A new developer picking up a feature in progress can go to Jira to read the details and also find the feature easily. Feature branches should be named with a prefix of feature/ and then, ideally, start with the name of the issue they refer to. So Jira issue abc-123 which is to add a search box would be created in feature branch called feature/abc-123-add-search-box.

Release branches

In the standard Gitflow model, when a feature is finished and ready for production then it should be merged back into develop and then deleted. However, sometimes your client or project will need to release only a subset of the total completed features to production. If they are all merged into develop then it will be a complex task to separate out the features or remove them from develop.

Instead, the features can be left in their feature branches until they are ready for release. At this point a new release branch is created from develop. The release branch should be named with the prefix release/ and finish with the next tag release number (e.g. if the last release was 1.5.1 then this will be 1.6.0 giving a release branch called release/1.6.0). The release branch starts like develop, but then each feature for release is merged into it.

The release branch can then be placed onto a staging site for final testing with all the new features ready for release to be tested. Once everyone is happy for release, the release branch is merged into develop and into master and the feature braches in it should be deleted.

A tag can then be cut from master with the name 1.6.0 and released to production.

Sounds hard - anything that can help me?

Yes, there is Gitflow tool which enhances the basic git command line tool to add in additional git flow commands which does some of the things above for us.

To make a git repo ready for gitflow you woud type in git flow init and then just press enter to everything it asks as the defaults are as described here.

To start a new hotfix you woud type git flow hotfix start 1.5.1 to create the hotfix branch cut from master and immedialtey switches to it.

To finish a hotfix you would type git flow hotfix finish 1.5.1 which merges the hotfix into master and develop and creates a tag release from master called 1.5.1

To start a new feature you would type git flow feature start abc-123-new-search-box to create the feature branch cut from develop and immedialtey switches to it.

To start a new release you would type git flow release start 1.6.0 create the feature branch cut from develop and immedialtey switches to it.

To merge feature branches into the release you can just use the basic git command git merge feature/abc-123-new-search-box

To finish a release you would use git flow release finish 1.6.0 which merges the release with all the features into develop and into master then deletes the release branch as it is no longer needed and creates a 1.6.0 tag from master. You would need to manually delete the now released feature branches git branch -D feature/abc-123-new-search-box

The command git flow feature finish abc-123-new-search-box can also be used which merges the feature into develop then deletes the feature branch in one go but we don't do this prefering to create release branches and then manually deleting the feature branches because we often have to release subsets of finished features.

Summary

Hopefully this has shown you how you can manage your workflow more effectivley using the Gitflow branching strategy. Below is a series of frequently asked questions developers new to Gitflow may find useful as a reference.

After some use of this with Drupal you might start to wish you could branch your database as easily as the code base allowing you to work on different db versions as well. We're working on some potential tools for this so stay tuned!

Gitflow FAQ I have just joined a project and checked out the repo. Do I need to enable/initiate Gitflow?

Yes you do for each project you checkout. The Gitflow tools manage the Gitflow workflow locally.

How do I initialise Gitflow locally?

While somewhere in the repo run: git flow init

You can accept all the defaults

If Gitflow is being enabled for the first time for the project (no just your local repo checkout) then it will be necessary to push back to the remote the master and develop branches.

I am ready to start a new feature. Should I do it in a new Gitflow feature branch?

Yes, always.

I am ready to start a new feature in a new Gitflow feature branch. What do I do next?

Firstly, you should make sure you have “init’d” Gitflow locally (see above).

Next you should ensure your master and develop branches are up-to-date with their remote counterparts. If you are unsure how to do this see “How can I ensure the Gitflow master and develop branches are up-to-date?”.

Ensure you know what the feature branch name will be. If you are unsure of our naming conventions that skip forward to “What/how should I name my new feature branch?”

You should be on the develop branch at this point and should ensure you local Drupal DB is up-to-date with the develop branch. If you are unsure how to check if your local Drupal DB is up-to-date with the develop branch see “How can I ensure my local Drupal DB is up-to-date with develop branch?”.

Now execute: git flow feature start <your-branch-name-here>

You should now commit your changes to this feature branch.

I have completed my feature and committed all my changes to my Gitflow feature branch. What do I do next?

Primarily you should ensure you have pushed the branch and all changes up to the remote.

This is dependent upon the project and the workflow agreed by the team. Let us assume that the project is in production, that we are in a support cycle, and that we are adopting a “continual release” workflow ...

See if there is a release branch and create one if not. This is essentially a place where we can merge features that we are ready to release to a the client for feedback/sign-off/review.

Checkout the release branch and ensure it is up-to-date with upstream changes in the remote repo. If you are unsure how to do this then see ”Ensuring my release branch is up-to-date with the remote release branch”.

Now you can merge you feature branch into the release branch with the command: git merge feature/my-feaure-branch

Finally you should push your changes from the feature branch back to the remote using the command: git push

Note: There should never be code in the release branch which is not in either a feature branch or the develop branch.

The client has reviewed my feature and would like additional changes. What should I do?

Make the relevant changes and commit into the feature branch you created originally for the work. Push the branch and changes back to the remote repository. Checkout the release branch, ensure it is up-to-date, merge in your feature just as your did before, and push the release branch back to the remote.

I have received sign-off from the client should I merge my feature into a release?

This is a decision of the project team to decide if it is the right time to merge your feature into the active release branch. If the client's happy then thats usually a good sign.

How should I merge my feature into the develop branch?

Generally this is frowned on. We normally merge our features into the active release branch.

If you must merge into develop then there are two ways. One is to “finish” the feature with Gitflow. Be warned that by default Gitflow will remove a feature branch locally as part of its clean-up upon the merge completing. It is strongly advised that you suppress this with the “keep” option and delete the branch manually when it is no longer of any use. This is a safeguard to ensure if finishing a feature should fail, that the feature branch is still available to attempt finishing again.

The optimal command for finishing a feature through Gitflow is:

git flow feature finish --keep my-feature-name

The second way and this is my preference is that the feature is finished using a “squash” commit, as squash commits yield a very clean and clear Git log.  You can perform a squash commit by adding the --squash option to the previous command:

git flow feature finish --keep --squash my-feature-name

Once you have committed your changes into the develop branch you should push the changes to the develop branch back to the remote repository with the command: git push origin develop

Finally, upon making changes to the develop branch, you should make sure to keep the release branch up-to-date. See “Ensuring the release branch is kept up-to-date with develop”.

Ensuring my release branch is up-to-date with the remote release branch

Find the release branch using git branch -r to list them all.

Checkout your local release branch with the command: git checkout release/[release number]

You should ensure your local refs for the release branch are up-to-date which you can do with the following command: git fetch origin release/[release number]

If you local release branch is “behind” the remote ref then you should bring you local branch up-to-date with the remote branch using the command: git merge --ff origin/release/[release number]

Ensuring the release branch is kept up-to-date with develop

If you have just committed a changes into the develop branch then it is paramount that the release branch is kept up-to-date with those changes.

To do this checkout the release branch and ensure it is up-to-date. If you are unsure how to do this then see ”Ensuring my release branch is up-to-date with the remote release branch”.

You can now merge the changes from develop into your release branch with the following command: git merge develop

Once the changed from develop have been merged into the release branch you should push the changes to the release branch back to the remote repository with the following command: git push origin release

Can I commit my changes for multiple JIRA issues, Basecamp to-dos, etc into the same Gitflow feature branch?

No. It would defeat the point of having a feature/bugfix/unit of work per feature branch, which is important to allow us to easily merge/release individual features/bugfixes/unit of works as they are completed and approved by the client.

I am using Gitflow is it ever acceptable to commit a change directly to master?

Never. Ever (ref Sean Bean above)

I am using Gitflow is it ever acceptable to commit a change directly to develop?

As a general rule, no. However, there are a few cases where it may be the right thing to do. Discuss the scenario with the project team if you think you should be committing to develop.

How can I ensure the Gitflow master and develop branches are up-to-date?

While somewhere in the repo filesystem you can issue this command: git fetch --all

That will update all your local pointers (aka references/refs) to where in the graph the remote branches are currently. Then you can use PhpStorm’s version control log viewer, or Source Tree or Tig and see if you pointers for master and develop match the same commit in the graph as their remote counterparts.

If they are not then you can update as follows. We will assume the develop branch here for our example:

git checkout develop

git merge --ff origin/develop

What/how should I name my new feature branch?

Ideally, you will be have a JIRA ticket. If not, why not? If you have a Basecamp to-do and your project is in JIRA then why not create a new JIRA ticket? JIRA is great for managing issues, Basecamp is great for managing the client.

If you have got a JIRA issue then the issue will have a project short code as a prefix and then an issue number. For example DSD-140. You should name your Gitflow feature branch using the issue Id.

How can I ensure my local Drupal DB is up-to-date with develop branch?

Ensure the develop branch is up-to-date. Now run the following commands which make sure your local setup has the right modules enabled, has the latest db updates applied, has the latest feature changes applied and has a fresh, clean cache.

drush @vdd master-exec drush @vdd updb drush @vdd fra -y drush @vdd cc all

ERPAL: Drupal Security Expert Interview - David Snopek about secure Drupal sites

Planet Drupal -

David Snopek makes our "Drupal Security Expert Interview" series round! He is a founder of myDropWizard.com and a long-time Drupal developer and community member.

Among other things, he co-maintains the Panopoly distribution, is a member of the Drupal security team, and co-organizes the local Drupal meetup group in Milwaukee, WI.

Enjoy his expert estimation! Read more

Nacho Digital: First DrupalCamp Santiago/Chile

Planet Drupal -

Some notes about my attendance to the first DrupalCamp in Chile

It was a beautiful experience all the way. It was great to get together with people from all across Latin America, reconnecting with old friends, meeting new ones, and I must say the location (ECLAC) deserves an important mention. The building architecture is superb, the conference room amazing, and the infrastructure and support was great.

It was the first time I had the chance to give a couple of talks (Performance & Services) within the Drupal community. I really liked the experience, people seemed to like my talks too, so all good.

DrupalCon News: Where to go for adventures in India

Planet Drupal -

India is a massive country: with a population of over 1.2 billion and covering nearly 1.3 million square miles (~3.3 million square km), India is full of opportunities for adventure. With so much to choose from, we wanted to show the Druplicon experiencing some of the best that India has to offer. This comic strip is called Safar-e Drupal: the name is written in urdu, a language widely used in Indian Subcontinent. The word "safar" translates to "journey" in English, so the words "Safar-e-Drupal" is a way of saying that a journey and Drupal are interconnected.

Drupal Watchdog: VIDEO: Welcome to DrupalCon - The Barcelona Edition

Planet Drupal -

Did we have fun in Barcelona?
OMG, yes!

Did we eat all the tapas on the menu and wash them down with pitchers of sangria?
Yes indeed!

Did we meet a host of Drupalists, make new friends, network like crazy, and learn all kinds of Drupal stuff?
Yes-yes-yes and yes!

So now, relive the moment (or, if you missed it, see what it was like): the people, the places, the food, the whole DrupalCon Experience.

And, as a bonus: Are you thinking of attending DrupalCon Dublin (Sept. 26-30, 2016)? Need some convincing? Watch this video – and show it to your boss!

Thanks (in order of appearance):
Jeremy Andrews, Holly Ross, Rachel Friesen, Mikkel Mandel, Saket Kumar, Rakhi Mandania, Marja Doedens, Christian Wanscher, Verónica Vedia, Saran Raj, Shankey Thukral, Mudassar Ali, Kristof Vercruyssen, Cyrielle Charriere, Emmanuel Quedville, Fabian Franz, Jeff Sheltren, Dylan Clear, June Gregg, Mark Carver, Rudy Grigar, Amanda Gonser, Cathy Theys, Nancy Beers, David Archer, Sweta Shahi, Tony Williams, Steve Richards, Kate Marshalkina, Klaas Chielens, Kristof Van Tomme, Jared Smith, Antonella Severo, Hilmar Kári Hallbjörnsson, MortenDK, Anthony Lindsay, Stella Power.

Tags:  DrupalCon Barcelona Documentary Drupal Association Tag1 Consulting Drupal DrupalCon Barcelona Video: 

Vancouver & Montreal, Canada: CloudFlare's latest data centers

Cloudflare Blog -

With the holiday season in full swing, it's only fitting that we continue to spread cheer, joy and a faster Internet around the world. To start the season we begin in Canada with NHL rivals Montreal and Vancouver, our 70th and 71st points of presence (PoPs) globally. Montreal and Vancouver, the 2nd and 3rd largest Canadian metropolitan areas, respectively, join our existing PoP in Canada's largest, Toronto.

Together, CloudFlare's network in Canada is now milliseconds away from the country's 31 million Internet users. As of now, the web sites, mobile apps and APIs of all CloudFlare customers are delivered at a cool 6.1 million times the speed of the fastest slapshot (for the curious, the current NHL speed record belongs to Zdeno Chára of the Boston Bruins, whose slapshot clocked 108.8 miles per hour / 175.1 kilometers per hour).

Latency matters

Canada is not just one of the most wired countries in the world, with nearly 87 per cent of Canadian households connected to the Internet, but also one of the largest as measured by e-commerce transaction volume. According to Statistics Canada, Canadian enterprises sold more than US$100 billion in goods and services over the Internet in 2013, up from US$87 billion a year earlier. Interestingly, the median amount spent on retail e-commerce transactions is substantially higher for Canadian-domiciled web sites (including the Canadian arms of US e-commerce giants) than sites selling into Canada that are domiciled in the US. One significant factor mentioned in this dichotomy is latency.

Source: CIRA Tracking Research, March 2014

Latency matters. Almost seven years ago, Amazon published a remarkable statistic: the online retail giant found that every 100ms of latency cost them 1% in sales. Executives from Google and Microsoft presented data demonstrating that even small delays of under half a second impact business metrics. Today, a broker could lose millions in revenues per millisecond if their electronic trading platform is even a few milliseconds behind the competition. Of course, if you're a CloudFlare user you needn't worry about this.

We all win a fast Internet

Montreal and Vancouver are the first of several new data centers we have planned for this holiday season. While each new site is another step in our quest to conquer latency, be sure to also check out our HTTP/2 demo and our HTTP/2 guide for developers to further optimize your web applications on CloudFlare.

— Happy Holidays from the CloudFlare team

The CloudFlare network today: (to be updated soon with Vancouver & Montreal!)

The New Drupal 8 Configuration System

Lullabot -

Matt & Mike talk to Alex Pott, Matthew Tift, and Greg Dunlap about all things Drupal Configuration Management and their experiences as owners of Drupal 8's Configuration Management Initiative known as CMI.

Drupal Association News: 12 Days of Drupal Community: Dhruv Baldawa

Planet Drupal -

*/

The Drupal community is full of people who make the world a brighter place. This month we are sharing more about the community members participating in our membership drive in this blog series. We work together, we motivate each other, and we learn together. Thank you to our community for inspiring us every day.

Dhruv Baldawa (dhruvbaldawa)

About Dhruv

Dhruv is a developer who began his journey into Drupal as a Google Summer of Code student. He lives in India and received a DrupalCon scholarship in 2012 to travel to Denver, which suited him well as he is an avid traveler.

What Dhruv says about membership

When I first posted a draft proposal, I had no idea how Drupal worked. I was truly amazed at how welcoming, open and helping the community is. It helped me learn about the various parts of Drupal's codebase, the modules and other subsystems. It was due to Drupal that I landed my first job. Much of what I have achieved so far, I owe to Drupal and its amazing community, and it is real pleasure to be a part of it.

If you are not yet a member, join the Drupal Association and be part of our success together.

Personal blog tags: Membershipcommunitymembership 2015 series

Partial kernel bypass merged into netmap master

Cloudflare Blog -

In a previous post we described our work on a new netmap mode called single-rx-queue.

After submitting the pull request, the netmap maintainers told us that the patch was interesting, but they would prefer something more configurable instead of a tailored custom mode.

After an exchange of ideas and some more work, our patch just got merged to mainline netmap.

Meet the new netmap

Before our patch netmap used to be an all-or-nothing deal. That is: there was no way to put a network adapter partially in netmap mode. All of the queues would have to be detached from the host network stack. Even a netmap mode called “single ring pair” didn't help.

Our final patch is extended and more generic, while still supporting the simple functionality of our original single-rx-queue mode.

First we modified netmap to leave queues that are not explicitly requested to be in netmap mode attached to the host stack. In this way, if a user requests a pair of rings (for example using nm_open(“netmap:eth0-4”)) it will actually get a reference to both the number 4 RX and TX rings, while keeping the other rings attached to the kernel stack.

But since the NIC is still partially connected to the host stack, a new problem arises: what should we do with packets that are going to be transmitted by the operating system to a TX ring which is in netmap mode? The solution is simple: just move them to the RX host ring. In this way we can access these packets from netmap simply by opening the interface again in netmap mode and asking for its software ring pair.

Last, for simpler use cases we needed a way to ask for only the RX rings, without the TX counterpart - we do not need TX rings for our specific use case. To achieve this we introduced a couple of flags, NR_TX_RINGS_ONLY and NR_RX_RINGS_ONLY (which translate to /T and /R when we are using nm_open()) to request only TX or RX rings.

With these changes, the only line we needed to edit in our code was the netmap interface name passed to nm_open(). This:

snprintf(nm_if, sizeof(nm_if) “netmap:%s~%d”, if_name, ring_nr);

becomes this:

snprintf(nm_if, sizeof(nm_if), “netmap:%s-%d/R”, iface_name, ring_nr);

and everything kept working as expected!

Try it out

You can follow these instructions to build a test program under Linux. In this example we are using the ixgbe driver.

The test program source code is available on github:

First clone the test application and the netmap repository:

$ git clone https://github.com/cloudflare/cloudflare-blog $ cd cloudflare-blog/2015-12-nm-single-rx-queue $ git clone https://github.com/luigirizzo/netmap deps

build it:

$ make

build and load netmap:

$ cd deps/netmap/LINUX $ ./configure --kernel-sources=/path/to/kernel/sources --driver=ixgbe $ make $ sudo insmod netmap.ko $ sudo insmod ixgbe/ixgbe.ko

and launch the application:

$ sudo ./nm-single-rx-queue eth0 1 Thanks

We would like to thanks Luigi and Giuseppe for their great help shaping the final patch and their work on netmap.

Issue 220

The Weekly Drop -

Issue 220 - December, 17th 2015 I'll be taking a couple weeks off for the holidays so this will be the final issue of TheWeeklyDrop for 2015. It has been a great year for Drupal as well as TheWeeklyDrop. Thank you all for subscribing and here's to a great 2016. From Our Sponsor Drupal & Security: Find Out How to Secure Your Site for 2016

Security breaches are becoming more common, and Drupal has become a perfect target for automated attacks with its increasing popularity. But, with a solid knowledge of the most common security threats, developers can quickly identify those security issues and learn how to address them. Watch this recorded webinar to learn how Drupal can protect against DDoS attacks, configuration mistakes that make you vulnerable, security improvements in Drupal 8, and more.

Articles How I Learned to Stop Worrying and Love Code Reviews Manage Updates with Continuous Development The API Web Needs a GUI

Kristof Van Tomme makes the case for democratizing APIs with Drupal.

The Definitive PHP 7 (Final Version) & HHVM Benchmark

Take a look at how the most popular PHP based frameworks perform with PHP 7.

The Need for Web Developers Is Going Up - Way Up

"The USDOL is forecasting that the number of web developer positions will grow 26.6% (over 39,000 jobs) between 2014 and 2024."

Drupal 8 6 Reasons to Deploy Drupal 8 with Docker (+How to Guide)

Chingis Sandanov explains how you can build and deploy Drupal 8 with Docker containers.

Drupal 8 Queue API - Powerful Manual and Cron Queueing Lessons from Porting Entityqueue to Drupal 8

Mark Ferree gives us an inside look at porting a module to Drupal 8.

Speak HTTP with Drupal::httpClient

William Hetherington shows us how we can use Guzzle in Drupal 8.

Using Twig in Drupal 8

Side by side comparisons for PHPTemplate(Drupal 7 theme engine) and Twig.

Video: Drupal 8 Build Process with Vagrant and Docker

A nice look inside the way Activelamp is building Drupal 8 development environments.

Tutorials How to Create a CKeditor Plugin for Drupal 7

Israel Morales show us how to customize the CKeditor with a custom plugin. It's not as difficult as you might think.

How to Set Up Auto Posting from a Drupal Site to Facebook Video: What Are Loops in Drupal Rules? Projects Commerce 2.x Stories: Workflows

Bojan Zivanovic explains the new approach to checkout statuses in Commerce 2. The improvements to Commerce are making it an even better option for ecommerce.

Drush Commands (Now Includes Drush 8) How to Create Footnotes in Drupal

Steve Burge shows us how to use the Footnotes module.

Monitoring Drupal Site Status at Scale

PreviousNext introduces a new project called Dashboard Connector with a different approach to monitoring your Drupal sites.

Releases Agov 7.x-3.0 Bootstrap 7.x-3.3 Bootstrap 8.x-3.0-Beta1 Breakpoints 7.x-1.4 Ds 8.x-2.2 field_group 8.x-1.0-Rc4 login_destination 7.x-1.3 migrate_ui 8.x-1.0 Tfa 7.x-2.0-Beta3 Workflow 7.x-2.7 Podcasts Art, PHP, Sculpin & More at SymfonyLive Berlin 2015 - Acquia Podcast DrupalEasy Podcast 166 - Haulin' Cats (Mike Bell - Mental Health and Open Source) News 2016 Drupal Association Board Election Details What’s New on Drupal.org? - November 2015

Tim Lehnen gives us the lowdown on the happenings on Drupal.org.

Jobs List Your Job on Drupal Jobs

Wanna get the word out about your great Drupal job? Get your job in front of hundreds of Drupal job seekers every day at Jobs.Drupal.Org.

Featured Jobs Senior Drupal Developer

Salsa Digital Collingwood/VIC/AU

Lead Web Developer

American Medical Association Chicago/IL/US

Drupal Developer

American Medical Association Chicago/IL/US


Miro Michalicka: Custom blocks in Drupal 8

Planet Drupal -

You have probably notices that CMI has some limitation in exporting custom blocks. These are unfortunately quite important for Front enders and also for client. Let me show you, how you can simply create custom blocks in code during installation of your module.

If you are reading this blog, I assume you know that blocks are finally entities in Drupal 8. You can define your own block type and add some fields into it. For simple demonstration I’m using predefined custom block type Basic block with body text field. Same approach can be of course used for your custom block types with many additional fiels.

First you need to create Custom block:

1 <?php 2 3 use Drupal\block_content\Entity\BlockContent; 4 5 /** 6 * Implements hook_install(). 7 */ 8 function awesome_blocks_install() { 9 $block_content = BlockContent::create([ 10 'type' => 'basic', 11 'info' => 'About us', 12 ]); 13 $block_content->set('body', 'Some long text about us...'); 14 $block_content->save(); 15 }

If you install your module now, you can find this block in Custom block library. You can update all fields which is big change since Drupal 7, but you still need to show your block somewhere on page. Because block are entities, they can be displayed multiple times. You need to create block instance which can be placed into region and visibility settings can be applied.

1 <?php 2 3 use Drupal\block\Entity\Block; 4 use Drupal\block_content\Entity\BlockContent; 5 6 /** 7 * Implements hook_install(). 8 */ 9 function awesome_blocks_install() { 10 $block_content = BlockContent::create([ 11 'type' => 'basic', 12 'info' => 'About us', 13 ]); 14 $block_content->set('body', 'Some long text about us...'); 15 $block_content->save(); 16 17 $block = Block::create([ 18 'id' => 'about_us', 19 'plugin' => 'block_content:' . $block_content->uuid(), 20 'region' => 'header', 21 'provider' => 'block_content', 22 'weight' => -100, 23 'theme' => \Drupal::config('system.theme')->get('default'), 24 'visibility' => array(), 25 'settings' => [ 26 'label' => 'About us', 27 'label_display' => FALSE, 28 ], 29 ]); 30 $block->save(); 31 }

Now you just need to put this code into your module’s .install file and enable it. Enjoy :)

Miro Michalicka: Custom blocks in Drupal 8

Planet Drupal -

You have probably notices that CMI has some limitation in exporting custom blocks. These are unfortunately quite important for Front enders and also for client. Let me show you, how you can simply create custom blocks in code during installation of your module.

If you are reading this blog, I assume you know that blocks are finally entities in Drupal 8. You can define your own block type and add some fields into it. For simple demonstration I’m using predefined custom block type Basic block with body text field. Same approach can be of course used for your custom block types with many additional fiels.

First you need to create Custom block:

1 <?php 2 3 use Drupal\block_content\Entity\BlockContent; 4 5 /** 6 * Implements hook_install(). 7 */ 8 function awesome_blocks_install() { 9 $block_content = BlockContent::create([ 10 'type' => 'basic', 11 'info' => 'About us', 12 ]); 13 $block_content->set('body', 'Some long text about us...'); 14 $block_content->save(); 15 }

If you install your module now, you can find this block in Custom block library. You can update all fields which is big change since Drupal 7, but you still need to show your block somewhere on page. Because block are entities, they can be displayed multiple times. You need to create block instance which can be placed into region and visibility settings can be applied.

1 <?php 2 3 use Drupal\block\Entity\Block; 4 use Drupal\block_content\Entity\BlockContent; 5 6 /** 7 * Implements hook_install(). 8 */ 9 function awesome_blocks_install() { 10 $block_content = BlockContent::create([ 11 'type' => 'basic', 12 'info' => 'About us', 13 ]); 14 $block_content->set('body', 'Some long text about us...'); 15 $block_content->save(); 16 17 $block = Block::create([ 18 'id' => 'about_us', 19 'plugin' => 'block_content:' . $block_content->uuid(), 20 'region' => 'header', 21 'provider' => 'block_content', 22 'weight' => -100, 23 'theme' => \Drupal::config('system.theme')->get('default'), 24 'visibility' => array(), 25 'settings' => [ 26 'label' => 'About us', 27 'label_display' => FALSE, 28 ], 29 ]); 30 $block->save(); 31 }

Now you just need to put this code into your module’s .install file and enable it. Enjoy :)

Pages

Subscribe to Cruiskeen Consulting LLC aggregator