Showing posts with label drupal 7. Show all posts
Showing posts with label drupal 7. Show all posts

Thursday, January 28, 2016

Drupal 7: Set Default Image to Content with Update Hook


Here is the update hook to add default image to article content:

/**
 * Update default image on field base.
 */
function mymodule_update_7300() {
  $field_name = 'field_fb_article_image';
  $filename = 'images.png';
  $source = drupal_get_path('module', 'mymodule') . '/includes/images' . '/' . $filename;

  // Load field info.
  $field = field_info_field($field_name);

  // Create new file object and get new fid.
  if (file_exists($source)) {
    $file = new stdClass;
    $file->filename = $filename;
    $file->timestamp = REQUEST_TIME;
    $file->uri = $source;
    $file->filemime = file_get_mimetype($source);
    $file->uid = 1;
    $file->status = 1;
    $file = file_copy($file, 'public://files', FILE_EXISTS_REPLACE);
    $fid = $file->fid;

    // Replace old fid with new fid.
    $field['settings']['default_image'] = (string) $fid;

    // Update field.
    field_update_field($field);
  }

}

Sunday, May 12, 2013

How to use xmlrpc in Drupal 7


I'm new to Drupal but after grance thru many article, this is the most easier for me to understand.

Here is the references link: http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_xmlrpc/7

But you may read my finding too. :)

Step 1:
Add the piece of code in sites\yourproject\modules\custom\yourmodule

function yourmodule_xmlrpc() {
  return array(
    'yourmethodName' => 'yourfunctionName',
    array(
      'yourmethodName',      'yourfunctionName',      array(  // The element might be diff and it base on application call back and answer
        'boolean',  //The first element of this array is the type of return value 
        'string',   //You can do just answer or more elements
        'string',
        'string',
        'string',
        'string',
      ),
      t('Handling ping request'),
    ),
  );
}

Step 2:
Add the piece of code in sites\yourproject\modules\custom\yourmodule
// Assume you need to save parameter4 which is array into node table
function yourfunctionName($parameter1, $parameter2, $parameter3, $parameter4) {        
      foreach($parameter4 as $arr){
            $node = new stdClass();
            $node->type ='contenttypename';
            $node->uid = 1;
            $node->title = $arr['title'];
            $node->status;
            $node->body[$node->language][0]['format'] = 'full_html';
            $node->body[$node->language][0]['summary'] = text_summary($arr['body']);
            $node->body[$node->language][0]['value']   = $arr['body'];
            if($node = node_submit($node)){
                 node_save($node);
           }
       }
      $answer = array('code', 'record saved');
      return $answer;   // 
}