GithubHelp home page GithubHelp logo

drupal-cheat-sheet's Introduction

Drupal Cheat Sheet

Table of contents

Introduction

The goal of this cheat sheet is to help as a general pointer to push you in the correct direction for various implmenetation and the best way to do it. E.g. an OOP orienter solution will be preferred in this cheat sheet over an none OOP solution. Not everything in this guide will have code examples, some items will just contain a summary and/or a link a webpage or other reference site.

Contributing If you think something is missing or incorrect, don't hesitate to create a issue in the queue so I can have a look. Even better would be if you created a pull request and helped actively to improve this cheat sheet ๐Ÿ˜„.

General references

Links

API references

Custom Node Access Permissions

Access Grants (reccomended implementation)

Grants are fairely easy to understand, but can be quite a bit more tricky to get working. Depending on your use case, you'll need to implement 2 to 3 hooks to get this working. For a concrete example and the full documentation on the hooks visit the node.api.php file in your Drupal installation.

First step is to implement hook_node_access_records() who will define the permissions needed to access a node.

/**
 * Implements hook_node_access_records().
 */
function mymodule_node_access_records(NodeInterface $node) {
  if ($node->getType() === 'foo') {
    $grants[] = [
      'realm' => 'foo_realm',
      'gid' => 1,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ];
    return $grants;
  }
}

Second step is to implement hook_node_grants() who will give grants to a user.

 /**
 * Implements hook_node_grants().
 */
function mymodule_node_grants(AccountInterface $account, $op) {
  $grants = [];
  if ($op === 'view' && strstr($account->getEmail(), '@foobar.com') {
    // The '1' matches the GID of the grant.
    $grants['foo_realm'] = [1];
  }
  return $grants;
}

Third step is to implement hook_node_access_records_alter() if you're in need of altering the existing grants that other modules might set.

/**
 * Implements hook_node_access_records_alter().
 */
function mymodule_node_access_records_alter(&$grants, Drupal\node\NodeInterface $node) {
  if ($node->getType() === 'foo') {
    $grants = ['foo_realm' => $grants[1]];
  }
}

TIPS:

  • Don't forget to rebuild the Node Access Permissions (/admin/reports/status/rebuild)
  • Inspect the database table node_access to see which grants are being set for all the nodes.
  • Have a read through the hooks documentation in the node.api.php file!

More examples:

hook_node_access() --> Not recommended

This might seem like the ideal solution, but note that this is very memory intensive if you're loading large lists of nodes in for example a view or something. For this reason every developer shoud limit the usage of hook_node_access() on their projects.

/**
 * Implements hook_node_access().
 */
function mymodule_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $type = $node->getType();
  if ($type === 'foo' && $op === 'view') {
    // Only allow users with a specific email.
    if(strstr($account->getEmail(), '@foobar.com')) {
        return AccessResult::allowed();
    }
    return  AccessResult::forbidden();
  }
  return AccessResult::neutral();
}

Custom Form Validation

// todo: Add constraint + example.

Update hooks

Execute specific update hook (again)

This is possible by triggering the update hook using drush.

drush php-eval "module_load_install('MYMODULE'); MYMODULE_update_NUMBER();"

drupal-cheat-sheet's People

Contributors

bramdriesen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

jiisuominen

drupal-cheat-sheet's Issues

Contributing / Logo

Hi, I just stumbled on this via Google and would love to contribute some to the documentation as well as making a fun logo. Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.