I've created a module for a custom field type which selects images from cloudinary.
Is there a way to dynamically add entries as part of an import script? I've tried updating the database directly, but this is not ideal as it does not store any relations.
There is an answer for something similar, however I want to relate mine to an existing node. Is it possible with Node Export? If not, what am I doing wrong here:
My schema:
function cloudinary_field_schema() {
$columns = array(
'publicID' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'title' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'caption' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
'url' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'credit' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'keywords' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
'description' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
'primaryImage' => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE)
);
return array(
'columns' => $columns
);
}
My hook menu contains this:
function cloudinary_menu() {
$items['cloudinary/add'] = array(
'access callback' => TRUE,
'page callback' => 'add_image'
);
}
And bad practice importer:
function add_image () {
$new_image = array();
//get post
$entity_id = $_POST['entity_id'];
$publicID = $_POST["publicID"];
$url = $_POST["url"];
$title = $_POST["title"];
$caption = $_POST["caption"];
$description = $_POST["description"];
$keywords = $_POST["keywords"];
$credit = $_POST["credit"];
$primaryImage = $_POST["primaryImage"];
//validate
$entity_id = htmlspecialchars( $entity_id );
$publicID = htmlspecialchars($publicID);
$url = htmlspecialchars($url);
$title = htmlspecialchars($title);
$caption = htmlspecialchars($caption);
$description = htmlspecialchars($description);
$keywords = htmlspecialchars($keywords);
$credit = htmlspecialchars($credit);
$primaryImage = htmlspecialchars($primaryImage);
//add to new image
$new_image["entity_id"] = intval($entity_id);
$new_image["field_cloudinary_publicID"] = $publicID;
$new_image["field_cloudinary_url"] = $url;
$new_image["field_cloudinary_title"] = $title;
$new_image["field_cloudinary_caption "] = $caption;
$new_image["field_cloudinary_description"] = $description;
$new_image["field_cloudinary_keywords"] = $keywords;
$new_image["field_cloudinary_credit"] = $credit;
$new_image["field_cloudinary_primaryImage"] = $primaryImage;
$new_image["delta"] = 0;
//establish if this is a valid node using entity_id
$result = db_query('SELECT * FROM {node} n WHERE n.nid = :nid', array(':nid' => $new_image["entity_id"]));
//if it is, update cloudinary
if($result->rowCount() > 0) {
$r = $result->fetchAssoc();
$new_image["entity_type"] = "node";
$new_image["bundle"] = $r["type"];
$insert_id = db_insert('field_data_field_cloudinary')->fields($new_image)->execute();
drupal_json_output($insert_id);
} else {
drupal_json_output(false);
}
drupal_exit();
}
I'm very new to Drupal so struggle a lot with this stuff! Any help greatly appreciated!