0

I'd like to be able to programmatically create and delete Forums on a Drupal 7 install, via the Services module. I want to send in HTTP requests and have those things happen.

I know how to create a node (page, article, or forum topic). I know how to delete a node.

How can I create a forum? I think Forums is a taxonomy_vocabulary. I think creating and removing forums is equivalent to adding and removing terms to the vocabulary. But I would like to see an example.

Any help?

1 Answer 1

0

Part one: How to delete a forum.

curl -i -X DELETE \
  -H Cookie:SESSe6abeefcafe=ovQpO6ahoQYUUREX23bre_2Q1onya55i9fbmzq7TWEE \
  -H X-CSRF-Token:v6j7r9Rbase62tokenHere-m9YYZA \
  -H Accept:application/json \
  http://myserver/rest/taxonomy_term/7

Some notes:

  • to construct the URL, you need to use the Term ID of the particular forum you would like to delete
  • You need to use the Cookie obtained from the prior call to /user/login
  • You need to use the CSRF token obtained from that same call

Where does the term ID come from?

The term id in my case, is something I retrieved from a combination of two other calls: one to retrieve the taxonomy id for the forums, then another to retrieve the terms under that taxonomy. Like this:

curl -i -X GET \
  -H Cookie:SESSe6abeefcafe=ovQpO6ahoQYUUREX23bre_2Q1onya55i9fbmzq7TWEE \
 'http://myserver/rest/taxonomy_vocabulary?parameters\[machine_name\]=forums' 

The response is like this:

[{
  "vid": "1",
  "name": "Forums",
  "machine_name": "forums",
  "description": "Forum navigation vocabulary",
  "hierarchy": "0",
  "module": "forum",
  "weight": "-10",
  "uri": "http://myserver/rest/taxonomy_vocabulary/1"
}]

And then, with the taxonomy ID returned there, I can retrieve the terms, which correspond to actual forums, like this:

curl -i -X GET \
 -H Cookie:SESSe6abeefcafe=ovQpO6ahoQYUUREX23bre_2Q1onya55i9fbmzq7TWEE \
 -H Accept:application/json \
 'http://myserver/rest/taxonomy_term?parameters\[vid\]=1' 

The response is an array of JSON items, each of which has a term id. You can then choose one of those term id's to delete, as above.

Part two: How to create a forum.

To create a forum, do the converse; add a new term in the same "forums" vocabulary. like this.

curl -i -X POST \
  -H Cookie:SESSe6abeefcafe=ovQpO6ahoQYUUREX23bre_2Q1onya55i9fbmzq7TWEE \
  -H X-CSRF-Token:v6j7r9Rbase62tokenHere-m9YYZA \
  -H Accept:application/json \
  -H content-type:application/json \
  http://myserver/rest/taxonomy_term \
  -d '{
    "vid": "1",
    "name": "Another Forum on the site",
    "description": "",
    "format": null,
    "weight": "10"
  }'

The vid property needs to be the "vocabulary id" of the forums vocabulary. The uuid and the tid of the newly-created term will be generated for you. Unfortunately, the tid will not be returned for you to reference. You need to query again to find it.

curl -i -X GET \
  -H Cookie:SESSe6abeefcafe=ovQpO6ahoQYUUREX23bre_2Q1onya55i9fbmzq7TWEE \
  -H Accept:application/json \
  'http://myserver/rest/taxonomy_term?parameters\[name\]=Another+Forum+on+the+site'

Response:

[{
  "tid": "36",
  "vid": "1",
  "name": "Another Forum on the site",
  "description": "",
  "format": null,
  "weight": "10",
  "uuid": "dcbe0118-c160-4556-b0b6-1813241bb851",
  "uri": "http://myserver/rest/taxonomy_term/36"
}]

Therefore, make sure you use unique names for these "terms".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.