Coder Social home page Coder Social logo

d7api's Introduction

d7api

An api wrapper for Drupal 7 and doing various common tasks programmatically

Node

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();

User

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();

Organic Groups

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);

d7api's People

Contributors

jkuchynka avatar

Watchers

 avatar James Cloos avatar

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.