Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit f3315e9

Browse files
Merge pull request #2835 from aaronschachter/json_node
Drush command to create campaigns from JSON
2 parents 43d94e7 + 4091ee8 commit f3315e9

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* Implementation of hook_drush_command().
5+
*/
6+
function dosomething_campaign_drush_command() {
7+
$items = array();
8+
// Name of the drush command.
9+
$items['campaign-create'] = array(
10+
'description' => 'Creates a campaign node from given JSON file.',
11+
'arguments' => array(
12+
'filename' => 'Name of the JSON file to read.',
13+
),
14+
'required-arguments' => TRUE,
15+
'callback' => 'dosomething_campaign_drush_campaign_create',
16+
'examples' => array(
17+
'drush campaign-create ../tests/campaign/campaign.json' => 'Creates a campaign node from contents of campaign.json.',
18+
),
19+
);
20+
return $items;
21+
}
22+
23+
/**
24+
* Callback for campaign-create command.
25+
*/
26+
function dosomething_campaign_drush_campaign_create($filename) {
27+
if ($string = file_get_contents($filename)) {
28+
$node = dosomething_campaign_create_node_from_json($string);
29+
$message = "Created node nid " . $node->nid . ".";
30+
return $message;
31+
}
32+
else {
33+
return "Invalid filename.";
34+
}
35+
}

lib/modules/dosomething/dosomething_campaign/dosomething_campaign.helpers.inc

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,16 @@ function dosomething_campaign_load($node, $public = FALSE) {
7979
$campaign->status = dosomething_campaign_get_campaign_status($node);
8080
$campaign->type = dosomething_campaign_get_campaign_type($node);
8181

82-
// Plain text fields.
83-
$fields_plain_text = array(
84-
'call_to_action',
85-
'starter_statement_header',
86-
);
87-
foreach ($fields_plain_text as $property) {
82+
// Plain text properties.
83+
$plain_text = dosomething_campaign_get_property_names_plain_text();
84+
foreach ($plain_text as $property) {
8885
$field_name = 'field_' . $property;
8986
$campaign->{$property} = $wrapper->{$field_name}->value();
9087
}
9188

92-
// Filtered text fields.
93-
$fields_filtered_text = array(
94-
'items_needed',
95-
'promoting_tips',
96-
'solution_copy',
97-
'solution_support',
98-
'starter_statement',
99-
'time_and_place',
100-
'vips',
101-
);
102-
foreach ($fields_filtered_text as $property) {
89+
// Filtered text properties.
90+
$filtered_text = dosomething_campaign_get_property_names_filtered_text();
91+
foreach ($filtered_text as $property) {
10392
$field_name = 'field_' . $property;
10493
if ($value = $wrapper->{$field_name}->value()) {
10594
// Store filtered text.
@@ -209,6 +198,59 @@ function dosomething_campaign_load($node, $public = FALSE) {
209198
return $campaign;
210199
}
211200

201+
/**
202+
* Creates and returns a campaign node from given JSON string.
203+
*/
204+
function dosomething_campaign_create_node_from_json($string) {
205+
$data = json_decode($string);
206+
$node = new stdClass();
207+
$node->type = 'campaign';
208+
$node->title = $data->title;
209+
// Set all plain text properties:
210+
$plain_text = dosomething_campaign_get_property_names_plain_text();
211+
foreach ($plain_text as $text_field) {
212+
if (isset($data->{$text_field})) {
213+
$field_name = 'field_' . $text_field;
214+
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = $data->{$text_field};
215+
}
216+
}
217+
// Set all filtered text properties:
218+
$filtered_text = dosomething_campaign_get_property_names_filtered_text();
219+
foreach ($filtered_text as $text_field) {
220+
if (isset($data->{$text_field})) {
221+
$field_name = 'field_' . $text_field;
222+
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = $data->{$text_field};
223+
$node->{$field_name}[LANGUAGE_NONE][0]['format'] = 'markdown';
224+
}
225+
}
226+
node_save($node);
227+
return $node;
228+
}
229+
230+
/**
231+
* Returns array of campaign property names which contain plain text.
232+
*/
233+
function dosomething_campaign_get_property_names_plain_text() {
234+
return array(
235+
'call_to_action',
236+
'starter_statement_header',
237+
);
238+
}
239+
240+
/**
241+
* Returns array of campaign property names which contain filtered text.
242+
*/
243+
function dosomething_campaign_get_property_names_filtered_text() {
244+
return array(
245+
'items_needed',
246+
'promoting_tips',
247+
'solution_copy',
248+
'solution_support',
249+
'starter_statement',
250+
'time_and_place',
251+
'vips',
252+
);
253+
}
212254
/**
213255
* Returns a loaded campaign $node's field_campaign_type value.
214256
*

tests/campaign/campaign.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"title": "Super Sweet Test Campaign",
3+
"status": "active",
4+
"type": "sms_game",
5+
"call_to_action": "Challenge friends to stand up to testing sandwiches.",
6+
"solution_support": "Sandwiches help self esteem and hunger."
7+
}

0 commit comments

Comments
 (0)