An api wrapper for Drupal 7 and doing various common tasks programmatically
Creating a node:
$node = Node::create('content-type');
$node->save();
echo $node->nid;
Sensible defaults are set for the node:
$node->title = ''
$node->language : 'und'
$node->uid : '1'
$node->user : 'admin'
$node->log : 'Created by D7api'
$node->status : 0
$node->comment : 0
$node->promote : 0
$node->sticky : 0
Create a node, set a title, and a cck text field value, and set to published
$node = Node::create('content-type');
$node->title = 'Test Node';
$node->status = 1;
$node->field_sub_title = "My sub-title";
$node->save();
echo $node->nid;
Loading a node, modifying a cck text field:
$node = Node::load(123);
$node->field_message = 'Updated message!';
$node->save();
Create a user with just an email address:
$user = User::create('[email protected]');
$user->save(); // If this user already exists, will throw exception
// Defaults that get set:
$user->name = '[email protected]'; // Email passed to ::create method
$user->mail = '[email protected]';
$user->pass = '';
$user->status = 1;
$user->init = '[email protected]';
$user->roles = array(DRUPAL_AUTHENTICATED_RID => 'authenticated user')
Load a user by uid, update username, email address
$user = User::load(123); // Will throw an exception if user doesn't exist
$user->name = 'newname';
$user->mail = '[email protected]';
$user->save();
Get current loggedin user:
$user = User::load();
echo 'Welcome, '. $user->name .'!';
Delete a user:
$user = User::load(321);
$user->delete();
Create a new og node (content type must already be setup as a group) and load into Og plugin
$node = Node::create('group-type');
$node->title = 'mygroup';
$node->save();
$group = new Og($node->nid);
Add user as member to the group:
$group->add_user(123);
Check user is added:
echo $group->has_user(123); // true
Remove user from group:
$group->remove_user(123);
Get all the users in the group:
$users = $group->get_users();
Get all the groups a user belongs to:
$groups = Og::get_user_groups(321);