2

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!

3
  • There is the ability to not only create a custom field type but to then add that field to an existing bundle (Article) of and entity type (Node). However, you are not limited to the Article Bundle of the Node Entity Type. Commented Aug 29, 2014 at 15:02
  • erm, not sure what you are saying? You say there is a way? Pray, do tell Commented Aug 29, 2014 at 15:15
  • Have you try the Feeds Module? You can create new fields on a content type instead of using custom fields. Then you can import your record or data through Feeds Module.
    – CocoSkin
    Commented Sep 26, 2014 at 20:25

2 Answers 2

3
+25

Just to double check: You have a content type with fields for your images from cloudinary, and you have nodes that already exist in your site that you need to get the data from Cloudinary into your node?

It looks like your looking up nodes through a query by the entity id (nid). Instead of doing that I would simply just load the node object by the node_load function (like node_load($entity_id). You should be able to then assign the value of cloudinary fields in the node (it's gonna be something like $node->FIELD_NAME['und'][0]['value']). Once you have all your custom fields inputed, just use node_save($node).

1

If you are not that familiar with Drupal try the data module. It will give you the possibility to add a custom table (custom entity) with the possibility to add fields to that new entity. So you will be able to store your custom data and create an entityreference field to the node you wanna reference.

Your Answer

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

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