Skip to content

Instantly share code, notes, and snippets.

View clrockwell's full-sized avatar
🏠
Working from home

Chris Rockwell clrockwell

🏠
Working from home
  • Vanderbilt University Medical Center
View GitHub Profile
@clrockwell
clrockwell / your_form_name.module
Created September 9, 2017 16:15
Drupal 8: altering a @FormElement - update password_confirm element to require 6 characters
<?php
/**
* Implements hook_element_info_alter().
*/
function scholarrx_user_registration_element_info_alter(array &$info) {
$info['password_confirm']['#process'][] = 'scholarrx_process_password_confirm';
}
function scholarrx_process_password_confirm(&$element, FormStateInterface $form_state, &$complete_form) {
@clrockwell
clrockwell / CommerceLicenseCodeSamples.php
Created September 2, 2017 20:42
Some Commerce License code samples
<?php
// Load all licenses for current user that grant a specific license
$existing = $this->entityTypeManager->getStorage('commerce_license')
->loadByProperties([
'license_role' => 'machine_name_of_role',
'uid' => \Drupal::current_user()->id()
]);
@clrockwell
clrockwell / CreateLicense.php
Last active September 2, 2017 19:58
A service for creating licenses programmatically
<?php
namespace Drupal\free_trial;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\user\RoleInterface;
/**
* Class CreateLicense.
*/
class CreateLicense implements CreateLicenseInterface {
<?php
namespace Drupal\require_login\EventSubscriber;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Path\PathValidator;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
<?php
/**
* snippets for dealing with custom entities
* Use dependency injection in actual code
*/
function d8_custom_entity_new_entity_form() {
$entity = \Drupal::entityTypeManager()
->getStorage('the_entity_type')
->create()
->setOwnerId(\Drupal::currentUser()->id());
{% set has_bg_colour = paragraph.field_background_colour.value %}
{#
We are going to check here if the paragraph bundle has a background colour
set. If so, we apply that background colour as a class on that paragraph bundle.
Then, if it has a background colour, we decide that we want to give it some
padding, so we also add a .has-bg class to it. If individual paragraph bundles
need to override that, we can use removceClass on their paragraph.html.twig files.
#}
<?php
namespace Drupal\mymodule\Plugin\Field\FieldWidget;
use Drupal\telephone\Plugin\Field\FieldWidget\TelephoneDefaultWidget;
/**
* Plugin implementation of the 'telephone_input_mask' widget.
*
* @FieldWidget(
<?php
/**
* Two hooks are necessary for adding an extra field to an entity display
*/
/**
* Implements hook_entity_view().
* Define the content that should be shown
*/
function hook_entity_view($entity, $type, $view_mode, $langcode) {
<?php
/**
* Implements hook_field_extra_fields().
* Add the code to Registration enabled entities
*/
function my_module_field_extra_fields() {
$extra = [];
foreach (entity_get_info() as $entity_type => $entity_info) {
foreach (array_keys($entity_info['bundles']) as $bundle) {
/**
* Use
*
* $header['row1'][] => ['data' => 'header-1-1', 'colspan' => 2];
* $header['row1'][] => ['data' => 'header-1-2', 'colspan' => 2];
* $header['row2'] => ['Total', 'Avg', 'Total', 'Avg'];
* // add in some colgroups for targeted styling
* $colgroups = array_fill(0, 2, ['attributes' => ['span' => 2]]);
* $rows = array_fill(0, 4, '10');
*