Node submission form in Drupal 7

Mar 19, 2014

This is an update to the post: http://ivansotof.com/2009/11/create-your-own-node-submission-form/

We will be creating a module called nodeapply. It will be a simple module that will take some values and create an unpublished node on the system.

1. Create the module skeleton

Go to sites/all/modules
Create a folder called nodeapply
Inside the folder, create a file called nodeapply.info
Open nodeapply.info and enter the following

name = Apply to something
description = Let users submit applications to something
package = Other
core = 6.x
dependencies[] = content

(more…)

Posted in:

Including native .so libraries in Android

Dec 2, 2013

After trying to convert some of my old Android projects I realized that the new Gradle build system doesn’t seem to include .so files in the build.

Some Stack Overflow posts suggest compiling the .so files into a jar using Gradle, as suggested in http://stackoverflow.com/questions/16683775/include-so-library-in-apk-in-android-studio

What worked for me is just creating a zip file with the native library. The structure of the file must be:

lib/armeabi/libaacdecoder.so

Then I just renamed the file as accdecoderso.jar, moved to /libs/ and added it to Gradle dependencies.

dependencies {
    compile files('libs/accdecoderso.jar')
}

This way you can keep your build.gradle file clean and it doesn’t have to compile jar files on every run.

Posted in:

MMDOC DB

Sep 9, 2013

logo_0

For the last two weeks I’ve been working on a database for the TCG title called Might & Magic: Duel of Champions. The main goal of the site is letting players browse cards, heroes, spells and all the elements they can find on the game as well seeing details and compare cards in order to build a better deck.

I decided to publish the site as a preview, though there are a lot of features I still have in mind:

  • Deck building
  • Better Filtering
  • Global search
  • More information by card
  • Card comparison

For now, feel free to visit the site and provide feedback.

Posted in:

Drush save nodes

Jun 27, 2013

I wanted to share a quick tip for people using Drush and needs a way to loop through nodes.

Drush offers the eval command, that though is pretty dangerous to use it can be extremely useful. Let’s say you add a computed field or a geolocation field on a site with thousands of nodes. In order to get geolocated, you need to re-save all nodes, which can be a lot of work. Or you can just use Drush to loop through them:

drush eval 'for($i=9000;$i<=9200;$i++) {$n=node_load($i); node_save($n);}'

This will loop through node ID 9000 to 9200. It will load the node object and re-save it. This will of course change the “updated date” on it.

Posted in:

Importing data from another database on Drupal 7

May 7, 2013

Here’s a quick tutorial on how to import data into a Drupal installation using a secondary database. I’ve done this by using an external PHP file located at the root of the Drupal installation and bootstrapping it. You can call it import.php.

All the magic happens by usingĀ db_set_active(‘secondary_database’), which connects Drupal to another database. Calling db_set_active() will switch you back to the default one. This can be configured in our settings.php file.

Let’s start with our settings file.

$databases = array(
	'default' => array(
		'default' => array(
			'database' => 'drupal_db',
			'username' => 'root',
			'password' => 'root',
			'host' => 'localhost',
			'port' => '',
			'driver' => 'mysql',
			'prefix' => '',
		),
	),
	'secondary_database' => array(
		'default' => array(
			'database' => 'secondary_data',
			'username' => 'root',
			'password' => 'root',
			'host' => 'localhost',
			'port' => '',
			'driver' => 'mysql',
			'prefix' => '',
		),
	),
);

Let’s do some testing first with our import.php.

<?php
// we need to bootstrap Drupal first
require_once 'includes/bootstrap.inc';
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
print "Importing Entries\n";
// connect to a different DB defined on your settings file.
db_set_active('secondary_database');
// let's get all the entries we need for now.
$query = db_select('tablename', 's')->fields('s', array());
$results = $query->execute()->fetchAll();
print_r($results);

To run this, we can use:

php import.php

This should output an array of objects coming from your second database.

Importing data

Now that we have tested our connection, we can finalize our script:

<?php

// we need to bootstrap Drupal first
require_once 'includes/bootstrap.inc';
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

print "Importing Entries\n";
// connect to a different DB defined on your settings file.
db_set_active('secondary_database');
// let's get all the entries we need for now.
$query = db_select('tablename', 's')->fields('s', array());
$results = $query->execute()->fetchAll();

// back to the default database and building nodes
db_set_active();
foreach ($results as $res) {
	$node = new stdClass();
	$node->title = $res->name;
	$node->language = LANGUAGE_NONE;
	$node->type = 'body';
	$node->uid = 1;
	$node->status = 1;
	$node->body['und'][0]['format'] = 3;
	$node->body['und'][0]['summary'] = $res->description;
	$node->body['und'][0]['value'] = $res->description;
	node_save($node);
}

And that’s it, now you should be able to import entries from other databases by bootstrapping Drupal and connecting to it temporarily. All the new content will be stored in a Drupal way.

This is a cross post that is also published at HybridForge Blog

Posted in: