rearchitecting for drupal 8 · pdf fileprolog

69
Rearchitecting for Drupal 8 By frob Welcome to the re-architecting for Drupal 8 talk You should be able to follow along by visiting the link below.

Upload: duongminh

Post on 18-Mar-2018

227 views

Category:

Documents


2 download

TRANSCRIPT

Rearchitecting for Drupal 8

By frob

Welcome to the re-architecting for Drupal 8 talkYou should be able to follow along by visiting the link below.

In case you are wondering

You are here

Prolog <-- You are here in the sessionWhy look at Drupal 8

Custom LMS Drupal 7 architectureFeatures

Content strategy

Modules used

Custom development

Considerations Building with Drupal 8Architectural differences

UI Changes

Module availability

Specifically What needs to change

Prolog <-- You are here in the sessionWhy look at Drupal 8Custom LMS Drupal 7 architectureFeaturesContent strategyModules usedCustom development

Considerations Building with Drupal 8Architectural differencesUI ChangesModule availabilitySpecifically What needs to change

Hello My Name Is Frank

I am a Father, and Technology Enthusiast.

Online my name is frob (IRC, d.o, github)

On Twitter I am @frobdfasMy Blog is

I work for Clarity Innovations Inc.

www.frobiovox.com

Hello my name is Frank Robert AndersonWeb development since high schoolBeen working in Drupal professionally for just shy of 10years.I have worked almost exclusively in the Education industryfor nearly 4 years.Online you can find me as frobIRCgithubdrupal.org

Twitter frobdfasblogwebsiteI work for Clarity Innovations.

Clarity Innovations is a professional services firm based inPortland, Oregon focused on providing K-12 and highereducation technology consulting to non-profits, schools, andcorporations.Our 25 employees combine leading edge technology anddesign skills with direct experience in the classroom anduniversity.We develop solutions for our clients including strategicconsulting, professional development and content creation,and engineering solutions such as apps, web apps, andwebsites to help improve the process and practice ofteaching and learning.

Why do this?

WhyThe big question is D8 or D7, this is a question that I haveto ask myself every time I am asked to do an estimate.How do we pick which version of Drupal to use?Let me GET THIS OUT THE WAY I will not be going overhard numbers for building with one version over the other. Iwill give you the only answer that I can give knowing what Iknow about your project's requirements. IT DEPENDS.

I create with Drupal 7.

I want to create with Drupal 8.

I want to know the differences betweencreating with Drupal 7 vs 8.

I would like to know how I can leverage thenew features of Drupal 8 in complicatedinformation systems.

I create with Drupal 7.I want to create with Drupal 8.I want to know the differences between creating with Drupal7 vs 8.I would like to know how I can leverage the new features ofDrupal 8 in complicated information systems.

Lets go from:

To:

Custom LMS Architecture

Learning Management SystemAn LMS is software that allows for the organization and delivery of courses or

training programs.LMS ArchitectureNeed to deal with some prerequisite information so we allunderstand the goals of the project that I used as my testbed.What is an LMS? An LMS is a Learning ManagementSystem.It is software that allows for the organization and delivery ofcourses or training programs. If you have usedbuildamodule.com or drupalize.me then you have used aLMS.

Traditional LMS

Curriculum or Lesson planUnit or ModuleLesson/Project/Assessment/Mini-lessons

Traditional LMS is based on a traditional course or lessonplanCurriculum or Lesson planCollection of Units or ModulesCollection of Lesson/Project/Assessment/Mini-lessons

Our custom LMS is structured

Module -> MenuSegments -> Nodes or Content TypeComponents -> Paragraphs Items

Our custom LMS is structured asModule -> MenuSegments -> NodesComponents -> Paragraphs Items

Lesson plan equivalents are built with select fields on thesegment nodes.

Basic Drupal ArchitectureIn order to make sure we are all on the same page movingforward I need to talk about some basic Drupal Architecture.If this is review for some of you, I am sorry for theunskippable tutorial level.

Basic Drupal Content Architecture

Entities

Fields

Variables

Basic Drupal ArchitectureIn order to make sure we are all on the same page movingforward I need to talk about some basic Drupal Architecture.If this is review for some of you, I am sorry for theunskippable tutorial level.Don't worry I will mostly just be talking about Entities, Fields,and Variables.

Entities

Entities are things in Drupal; things do not have to be nodes.

Entities are things in Drupal, starting in drupal 7 we gotcontent entities.In Drupal 6 custom things where typically nodes or totallycustomIn Drupal 7 the entity-field system brought the CCK into corewithout making everything a nodeDrupal 8 has a fully fledged entity api which is derived fromthe capabilities of the contrib entity api module for drupal 7.Users, Taxonomy Terms, and Nodes, are all Entities

Bundles and Fields

A bundle is a collection of fields instanceson an Entity

A field instance is a place for complex datain a Entity Bundle

A property is simple storage for Entity leveldata

An Entity can have Bundles to allow for multiple collectionsof fields instances on a single type of entity.Field instances are unique to that bundle.An Entity has properties which span all bundles.created datenode statuswho created are all properties

A field instance is a place for complex data in a EntityBundleA property is simple storage for Entity level data

Properties

Properties are linked to an Entity, such as Node. Fields are linked to the Bundle, suchas Content Type.

Properties are stored on the entity's table and fields are stored in their own table.

Properties are linked to an Entity, such as Node. Fields arelinked to the Bundle, such as Content Type.Properties are stored on the entity's table and fields arestored in their own table.

Variables

Variables are what Drupal 7 provides for configuration --when it isn't a custom table.

Variables are what Drupal 7 provides for configuration --when it isn't a custom table.

LMS Features

Support Backwards Design philosophy

Site FeaturesTraditional LMSCollection of Curriculum or Lesson plansCollection of Unit or ModuleCollection of Lesson/Project/Assessment/Mini-lessons

Curriculum Designer will often use a Backwards DesignphilosophyStarts Standards/Topical they with to teach

Goals of lesson planning

Engage

Explore

Explain

Elaborate

Evaluate

Using the 5 e's as the goals of lesson planningEngageExploreExplainElaborateEvaluate

LMS Lesson Goals

Teaching

Application

AssessmentThus Goals for Lesson Plan Features

Activate Prior knowledgeModeling or TeachingPracticeApplicationAssessment

Our custom LMS is structured as

module -> menusegments -> nodescomponents -> paragraphs items

Drupal 7 Modules Used

Frank's list of modern site building toolsModules UsedI gave a talk on modern Drupal 7 development at a meetupin Portland last year, and I am not going to go into greatdetail here. I wrote a blog post about it and if you would likemore details I suggest you look there.I will go over the modules that this site used (in its corefunctionality), what they do.Later I will go over how they relate to Drupal 8.Remember when building a site that the output that drupalgive us is always a suggestion. The important part of sitebuilding is giving the content team everything they need,you can leave it up to the developers and themers to makesure that the content gets displayed properly.

Paragraphs allows us to create collections of fields. Each collection type is its ownbundle and can have its own fields.

Paragraphs

Paragraphs -- If you are familiar with field collection thenthis module functions very similarly, with the main exceptionof the user being able to pick the bundle or paragraph typewhen creating content.

Named eForm in Drupal 8. Allows the use of the field api to create webform and havethe submissions be entities.

Entity Form

Entity Form -- In Drupal 8 this module was renamed toeform. The cool part of this module is that it allows us to usethe field api to create webforms. Unlike the webformmodule, the form submissions are entities. This gives usflexibility use in views or displaying them with multiple viewmodes.

Solves the problem of putting views into entities as pseudo-fields.

Eva

EVA -- Entity View Attachment. Allows us to embed viewsinto fields. Handy for things like Taxonomy term views oranywhere an ID can be used as an argument in a view.

This module allow users to flag things. Largest use is probably the "Flag this asinappropriate" functionality.

Flag

Flags -- This module allow users to flag entities. Largest useis probably the "Flag this as inappropriate" functionality.The last two modules don't have much to do with this siteper-se but they are fantastic modules that will really help asite-builder.

Coffee works like Unity for Ubuntu or Spotlight search for Mac, or the Start Menu inWindows. Just start typing and a list of options will be presented. Pick the option and

it will take you there.

Coffee

Coffee -- Speed up your Drupal navigation by typing whereyou want to go. Much like unity for Ubuntu or Spotlight forMac or Start for Windows, this module allows us to ziparound our site's admin interface with ease.

Check more than one box with a click and drag mechanic.

Speedboxes

Speedboxes -- Allows us to check multiple checkboxes atthe same time. Super useful for setting up permissions.

A word on Blocks

Bean is an acronym that stands for:

Block Entities Aren't Nodes.

BeanThis is not a block heavy siteIt works mostly as an app with the blocks only really beingused to place active menus on the screen.This is mainly because there are not many reusable piecesto the site. If it isn't a page level thing then it really isn't apart of this site.This site doesn't use blocks, but when I use blocks I buildthem with BEAN.Bean gives us all the functionality of building content typeswith nodes except with blocks instead.I normally add a content view for beans at/admin/content/blocks and I disable the default drupal blockmanagement stuff, with the exception of block placement.because that is still necessary.But again, this site didn't make heavy use of blocks.

I don't always use blocks, but when I do I use the beanmodule

LMS Content Strategy

Arguably, the most important part of a site.

Content StrategyNow thats out of the way we can talk about the site'scontent strategy.

13 paragraphs types and 1 content type

Three where interactive

Text response

Checkbox list

Drag and Drop

In the end our site had 13 paragraphs item bundles for 1content typeThree where interactiveText responseCheckbox listDrag and Drop

Text response

Checkbox list

Drag and Drop

The rest are multimedia/content.

Everything can be combined to form larger whole components.

Four where used for custom inter and intra modulenavigationThe rest where videos, image, and text.all can be combined to form larger whole components.

Linking the EntityForm Submission to the Paragraph item

Entity Machine Name

Paragraph Type text_response

EntityForm Type text_responseLinking the EntityForm Submission to the Paragraph item,each interactive paragraphs item had a corresponding entityform with an entity reference back to the paragraphs itemthat displayed the form. We used the paragraphs item'sbundle machine name to pick the bundle of the EntityFormto display.We put this all together, using Drupal's ingenious renderarray system that doesn't care if it is displaying a form, orcontent, or anything themable, we where able to HULKsmash the entityforms into the paragraphs item displays andsave any users response and know what they whereresponding to and who was responding to it.

Evaluation

Sharing via Flags on the paragraphs items

We also had to allow for self evaluation. Our client wantedusers to be able to share their responses and learn from theresponses of others. So we used flags that let the usersshare their responses, in turn if they share their responsethen they see their peer's responses and then they can edittheir old response and save a new one.Our initial expectation was that we would have to build outall the content in a spreadsheet and at one point I wasbuilding a migration to import all the content from a CSV.Choosing the Paragraphs module gave our content editorsan interface that allowed them to build the content in the siteas we where adding functionality.

Custom Development

Custom DevelopmentThe meat of how we are putting this together is in thehook_entity_view_alter implementation and thehook_form_alter implementation.

Pass the build to custom function

if (!empty($build['#bundle'])) { $bundle = $build['#bundle']; if (function_exists("_component_{$bundle}_alter")) { // We need to pass build in an array in order to trick call user func // into passing the build by reference. call_user_func("_component_{$bundle}_alter", array(&$build)); } } Pass the build to custom function.

Remember we used the bundle type machine names to addthe correct entity form types? We do that withcall_user_func which calls a function based on the name wegive it. We check beforehand if the function exists, this wayit is extensible without having to modify too much existingcode. So if we want to add another interactive element wecan do so without modifying anything to do with our currentinteractive elements.

Text response callback

/** * Implements custom _component_TYPE_alter(). */ function _component_text_response_alter(&$build) { if (isset($build[0]['#entity']) && !empty($build[0]['#entity' module_load_include('inc', 'entityform', 'entityform.admin' $entity_form_name = 'text_submissions'; $component_id = $build[0]['#entity']->item_id; $entity = $build[0]['#entity']; $form = _get_entityform(entityform_empty_load($entity_form_name), $form['field_component_submission']['#attributes']['class' // ... Other Stuff ... $form['field_text_response_submission']['#attributes']['class' $form_rendered = drupal_render($form); $build[0][] = array(

Text response callbackIn the case of the text response this checks for a validcallback and runs this code to include the entity form.Then in the form_alter we change the actual form options orthe text area label to use the correct text.Things get complicated at this point, because ajax.We have to ajaxify each of the paragraph items. I'm notgoing to go into code here because that would just beboring, and it is better documented elsewhere.An interesting thing about saving a response is, only after itis saved can we make the paragraphs item sharable. Thenafter it is shared only then can we make show the sharedresponses. All of these things required heavy developmentfor Drupal 7, and will likely also require lots of custom workfor Drupal 8 too.

Considerations Building a LMS with Drupal 8

Architectural differences

UI Changes

Module availability

Specifically What needs to changeArchitectureCustom DevelopmentContent strategyConsiderations Building with Drupal 8

Architectural differencesUI ChangesModule availabilitySpecifically What needs to changeArchitectureCustom DevelopmentContent strategy

General Drupal 8 Considerations

Setup the devel module

It is now cache rebuilding

Modules cannot be disabled

Enable Developer Mode

Beware of online sources

General Drupal 8 Considerationsdevel kint debugging DO NOT USE KUMO it doesn't workon real objectsno longer cache clear now cache rebuildingNo more module disable, uninstall or dieEnable developer modehttps://www.drupal.org/node/2598914Nice thing is that there is lots of information availablealready for Drupal8 the problem is that there is lots of badinformation available for Drupal 8. Check your sources anddo not expect everything you read to actually work. Evend.o documentation can be out of date. poll module example.

Architectural Differences

Configuration Entities

Semantic Versioning

Blocks

View Modes

New field types (entity reference, link,telephone)

and isn't easy to export and sync and the placement of theblock is configuration and is easy to sync. This can lead toembarrassing deployment gaffs. Where the placement of ablock that doesn't exist is possible. Here is a good articleabout avoiding that.http://valuebound.com/resources/blog/how-to-sync-blocks-between-different-environments-a-drupal-8-website

Display ModesContent View ModesIf you used the entity view modes modules or display suiteto provide view modes for content types and such, then youshould already be familiar with this. If you don't know whatview modes are, they are ways for providing different waysto display the same content. So for a node we could have ateaser or a full view mode or a grid view mode. Basicallyanything that the design called for. These existed for contentin Drupal 7 but they required a contrib module such as entityview modes or the display suite module to configure themwithout code. Drupal 8 gave a full fledged UI to work with.

Form View ModesAll that stuff I said about being able to display content indifferent configurable ways now also applies to forms withDrupal 8. So, as an example, let say you have a verycomplicated user registration form because your clientwants to collect a bunch of information about the user, butnone of it is required. Now you can create a new simplifiedform for adding users. You can do this in the UI in Drupal 8.

New field types (entity reference, link, telephone)

Things to google (many have been backported to Drupal 7)

Composer support

Toolbar update (Navbar in Drupal 7)

Responsive Image Styles (Picture Modulein Drupal 7)

File Entities

RESTful api

Experimental Core Modules

Content Moderation

etc...

Things to google (many have been backported to Drupal 7)Composer supportToolbar update (Navbar in Drupal 7)Responsive Image Styles (Picture Module in Drupal 7)File EntitiesRESTful apiExperimental Core ModulesContent ModerationWorkflowsField LayoutMigrate Drupal UIPlace Blocks <- outside in

Switching to semver should allow for smaller backwardcompatible changes.Testing infrastructure should ensure backward compatiblechanges are backward compatible. The DA infrastructureteam has made this a reality..

UI Changes

Good News! Not much has changed significantly in the UI. I guess that could be badnew too.

This is going to be brief, mainly because the UI changes areminimal.

Operations

New Drop down thingy for selecting what we are doing to a thing.

One change is Operations now go in a drop down with themaybe most common one first? Really I don't know how thatwas chosen so if someone here does know please tell meafterward.

Form Display Settings

Multiple entity form settings on the Manage form display page, this includes fieldwidget settings.

Another change you will find between D7 and D8 I havealready hinted at with the Form View. If you have a alternatenode form then you will find the option for editing that formview in "Manage Form Display" section. This also meansthat controls for the form widgets are configured on the formview display settings and not the "Manage Fields" page.Mange Fields is now entirely about the field and its storage.

Good news about learning curves

Good news, there are fewer changes for site builders whoare coming from Drupal 7 to worry about. Bad news is forsomeone coming from not Drupal the perceived Drupallearning curve could be intimidating.

Module Availability

Module AvailabilityLets acknowledge the elephant in the room.

Big problemEntityForm is not supported on Drupal 8Find an alternativeCustom Entity

Comment module

Contact module extras

Relation module

Fund the development of a stable release

module works well enough for this experiment, but if thiswhere a full client project we would have to explore someoptions.Find an alternativeCustom Entities are an optionComment moduleContact module extrasRelation moduleDidn't bring this module up in the modules used because wedidn't use it, but this is a really cool module. This modulehas the ability to supplant the functionality of flag andorganic groups. It adds the concept of entity endpointswhich allow us to make fieldable relations between entities.In the example of this LMS that entity would have all thefields for the response, an endpoint for the paragraphs item(renamed paragraph revision for Drupal 8), and an endpointfor the user.I have used this module for saving user state in js apps andfor keeping track of users completion status for resources incustom Professional Development portals.

Fund the development of a stable releaseWhile I do encourage finding alternatives, at this state inDrupal 8 development I would encourage us to choose thefinal option because frankly aside from the comment modulenone of the above options are any more stable than eformand more importantly Drupal 8 needs more stability incontrib.

Second problemParagraphs doesn't want me to referenceparagraphs

It is possible, but requires use of theEXPERIMENTAL field widget.

In this case the problem wasn't themodule but my own unfamiliarity and overconfidence. The answer was staring me inthe face.

Second problemParagraphs doesn't want me to reference paragraphsIt is possible, but requires use of the EXPERIMENTAL fieldwidget. The main reason this is EXPERIMENTAL is that theparagraphs modules is not mean to be used to reusecontent, that is more of a function of blocks. I spent moretime on this than I would have liked, due to some of Drupal8's UI changes that I didn't fully understand. But it turned outto be a non-problem.In this case the problem wasn't the module but my ownunfamiliarity and over confidence. The answer was staringme in the face.

So we do have an elephant, but it is just a baby elephant.

Specifically What needs to change

Not Much

What needs to change?not muchThis site works more like a an app than a traditional site,this means we have slightly different expectations whenchoosing a Framework.Drupal was chosen for its scaleability, strong user centric orpermissions based content model, high quality contribspace, and familiarity. As I said before I have been doingdrupal professionally for close to 10 years, so I am veryfamiliar with the project, community, and workflow. However,I am constantly looking into other technology and I havebuilt things with other tech when Drupal isn't the bestchoice, so this isn't a statement coming from the island.Drupal 8 has lots of new Features but the biggest advanceshave been in underlying architecture (with the content/configentity system) and development workflow (with the switch toSemantic Versioning).

Architecture & Content Strategy

Mostly the same

Same Features

Same Modules

Same Architecture

Same StrategyArchitecturally the site remains the sameThe site has the same features and the same modules areavailable to provide us with the same architecture andcontent strategy.It won't always be like this. But if you put the work intomaking sure you have a good content strategy in the firstplace it will be less likely that you will have to completely re-architect a site when you move platforms. This is whycontent strategy is important.

Custom Development

Biggest Changes

The largest change is the addition of developing a stablerelease for whatever we use for the response saving.

Custom Development

This is where the largest changes where made.

Custom developmentThis is where the largest changes where made, but maybenot where it was expected.

Not Much

We still use hook_entity_view_alter

We still use hook_form_alter

In Drupal 7 we accomplished the HULK SMASH part, wherewe smash the form from the entity form into the display ofthe paragraphs item, with a hook_entity_view_alterimplementation coupled with a hook_form_alterimplementation. In Drupal 8 with its Object OrientedArchitecture I was able to accomplish it in the exact sameway.

"But Wait" you say, "Drupal 8 is OOP we aren't supposed toput stuff into the .module file anymore. That is just there fordecoration!"Oh no, the joy of modifying aspects of the program withhooks is not gone in D8. Hooks are good things, and eventhough Drupal is want to change things with new versionand add modern development practices to Drupal, Drupalalso isn't going to ditch what is likely the most flexiblepattern of php development just because it stepped in someOOP.

Okay, really lots

But all good!

The Object Oriented Architecture is designed to makecommon things easier.For example.When building this with Drupal 7, we had to reimplementparts of the form builder from the entity forms module inorder to display entity forms programatically. This was likelydue to the incomplete nature of the entity api in Drupal 7.In Drupal 8 the entity api is far more flushed out. We still useEntity Field Queries to get the EntityForm Submission forthe current user and the current paragraph revision, but thistime Drupal has an api for creating the empty EntityFormsubmission object if the user has never submitted. And ithas an interface for building forms for Entities, new or old.Thus we can get a form for a new entity in the same way weget the form for the old entity, with all the right fields filled in.

Drupal 8 version:

// This creates a new eform submission. $eform_submission = \Drupal::entityTypeManager() ->getStorage('eform_submission') ->create(['type' => 'multiple_choice_question']); // This loads an existing eform submission. $submitted_eform = \Drupal::entityTypeManager() ->getStorage('eform_submission')->load(2); // Either way we build the form with a call to the entity.form_builder service.$form = \Drupal::service('entity.form_builder') ->getForm($submitted_eform); // Then we append it to the render array. $build[] = $form;

To build a new entity, this could be a node,eform_submission, or comment, We user theDrupal::entityTypeManager class and call the create methodor to load an existing one we use the same class with theload method and the entity id.Then we user the entity form builder service to build theform for the entity. I tested this with nodes as well. It is thesame generic interface for all content entities.

No more Drupal 7 non-existent entity api

Drupal 8 has intelligent use of OOP thatmakes site building easier

Over 100 lines of code is reduced to under10

No more Drupal 7 non-existent entity apiDrupal 8 has intelligent use of OOP that makes site buildingeasierOver 100 lines of code is reduced to under 10Mind Blown

Why look at Drupal 8

Custom LMS Drupal 7 architecture

Considerations Building a with Drupal 8Architectural differences

UI Changes

Module availability

Specifically What needs to change

Conclusion <-- Achievement unlocked

When are sprints?

April 24-27: Sprint Lounge at Baltimore Convention Center

April 28: Sprint Day - General Sprints, Mentored Core Sprint, First-Time SprinterWorkshop at Baltimore Convention Center

We also provide a 24-hour Sprinter Lounge that opens on April 24th at 7:00pm andwill close on Thursday, April 27th at midnight. The Sprinter Lounge will be located at

the Hilton Baltimore (401 West Pratt Street) in room Peale A-C.

Based on community feedback and input from the Sprint Leads, we understand theneed for 'shorter sprints with greater support', and as a result will not be hosting

Extended Weekend Sprints at this DrupalCon.

When are sprintsApril 24-27: Sprint Lounge at Baltimore Convention CenterApril 28: Sprint Day - General Sprints, Mentored CoreSprint, First-Time Sprinter Workshop at BaltimoreConvention CenterWe also provide a 24-hour Sprinter Lounge that opens onApril 24th at 7:00pm and will close on Thursday, April 27that midnight. The Sprinter Lounge will be located at theHilton Baltimore (401 West Pratt Street) in room Peale A-C.

Don't forget to give me feedback

node/17242

Help us get better.