Information for: DEVELOPERS   PARTNERS

Updating nodes programmatically

Fast Track to Drupal 8 Coding – Back to intro
Previous lesson - Unpublish nodes programmatically
Next lesson – Create terms programmatically

Understanding the lesson goal

A website has a user role named Moderated, and when this role is added to a user, all the nodes created by the user should be updated by appending MODERATED USER to the title of the nodes.

Implementation methods

Drupal Version Method
Drupal 7 node_load(), node_save()
Drupal 8 hook_ENTITY_TYPE_update() $node->set()

Using the Drupal 7 method

Add the following code to the lotus.module file:

/**  * Implements hook_user_update().
* If a user has a Role named 'moderated',
* append the words 'MODERATED USER' to titles of
* all the nodes authored by the user
*/

function lotus_user_update(&$edit, $account, $category)
{
    $uid = $account->uid;
    $role = user_has_role('moderated', $account);
    if($role)
    {
      $query = new EntityFieldQuery();
      $query = $query->entityCondition('entity_type', 'node')
      ->propertyCondition('status',1)->propertyCondition('uid', $uid);
      $nodes = $query->execute();

      foreach($nodes as $node)
      {

        $node = node_load($node);

        $old_title = $node->title;

        if(strpos($old_title,'(MODERATED USER)') == FALSE)
        {
            $node->title =  $old_title . " (MODERATED USER)";
            $node->save();
        }

      }
      drupal_set_message('All nodes of the user updated');

    }
}

Using the Drupal 8 method

  1. Create a role named Moderated on your website (machine name: moderated).
  2. Add a user.
  3. Create some nodes and set the Author for the nodes as the newly added user.
  4. In the lotus.module file, add the following code:
use Drupal\node\Entity\Node; use Drupal\user\Entity\User;

/**  * Implements hook_ENTITY_TYPE_update().
* If a user has a Role named 'moderated',
* append the words 'MODERATED USER' to titles of
* all the nodes authored by the user
*/

function lotus_user_update(\Drupal\Core\Entity\EntityInterface $entity)
{
    $user = User::load($entity->id());
    $role = $user->hasRole('moderated');
    if($role)
    {
      $query = \Drupal::entityQuery('node')->condition('uid', $user->id());
      $nids = $query->execute();
      $nodes = Node::loadMultiple($nids);
      foreach($nodes as $node)
      {
         $old_title = $node->title->value;
         if(strpos($old_title,'(MODERATED USER)') == FALSE)
         {
            $node->set("title", $old_title . " (MODERATED USER)");
            $node->save();

         }
      }

      drupal_set_message('All nodes of the user updated');
    }
}
  1. Enable the Lotus module (if its already enabled, do a Cache Rebuild).
  2. In the admin menu, click People, and then edit the user to add the Moderated role to the user account.

All of the nodes created by the user should now be appended with the words MODERATED USER.