Archive for the ‘Programming’ Category

h1

Put your Content Types with CCK fields into code [Drupal]

April 18, 2011

Putting a content type into code is a good thing to do since it then follows your module and means you don’t have to recreate content types on each instance of the site (e.g. dev, test and live)

To create basic content types in code, you can use the following code pattern in your mymodule.install file:

function mymodule_install() {
_mymodule_install_content_types();
}

function _mymodule_install_content_types() {
// Create Video Content type
$video_node_type = array(
'type' => 'vzaar_video_page',
'name' => t('Vzaar Video Page'),
'module' => 'node',
'description' => t('A Page containing a video stored on Vzaar'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
node_type_save((object)_node_type_set_defaults($video_node_type));
}

Now on installation of the module, mymodule, you will get a new content type called Vzaar Video Page. This only has the Title and Body fields though so isn’t very useful. Using CCK we can add new fields in the interface but how do we get those fields out into code?

One way is to use the content_fields function to get an instance of a field as an array. You can then use that array in your install file to recreate the content. However, running this function is hardwork becaue you’ll need to run it in drupal then copy and paste the data into your file. An alternative is to use Drush, which is a full drupal environment on the command line to execute the function for you, then redirect the output into you install file.

drush ev "var_export( content_fields( 'field_myfield_name' ) );" >> mymodule.install

Now edit mymodule install and you’ll see the array at the end of the file. Move it into your install function and wrap it in the function content_field_instance_create. To test it works, make an update function as well in your install file, delete the content type from your drupal and then run update.php (or drush updb)

You’re final install function might look something like this …

/**
* Implementation of hook_update_N
*
* Add Vzaar Video content type
*/
function mymodule_update_6201() {
_mymodule_install_content_types();
}

function mymodule_install() {
_mymodule_install_content_types();
}

function _mymodule_install_content_types() {
// Create Video Content type
$video_node_type = array(
'type' => 'vzaar_video_page',
'name' => t('Vzaar Video Page'),
'module' => 'node',
'description' => t('A Page containing a video stored on Vzaar'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);

node_type_save((object)_node_type_set_defaults($video_node_type));

// Create CCK field for media_vzaar
$vzaar_media_field = array (
'field_name' => 'field_vzaar_video_url',
'type_name' => 'vzaar_video_page',
'display_settings' =>
array (
'label' =>
array (
'format' => 'above',
'exclude' => 0,
),
'teaser' =>
array (
'format' => 'default',
'exclude' => 0,
),
'full' =>
array (
'format' => 'default',
'exclude' => 0,
),
4 =>
array (
'format' => 'default',
'exclude' => 0,
),
),
'widget_active' => '1',
'type' => 'emvideo',
'required' => '0',
'multiple' => '0',
'db_storage' => '1',
'module' => 'emvideo',
'active' => '1',
'locked' => '0',
'columns' =>
array (
'embed' =>
array (
'type' => 'text',
'size' => 'big',
'not null' => false,
'sortable' => true,
),
'value' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'provider' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'data' =>
array (
'type' => 'text',
'size' => 'big',
'not null' => false,
'sortable' => false,
),
'status' =>
array (
'description' => 'The availability status of this media.',
'type' => 'int',
'unsigned' => 'TRUE',
'not null' => true,
'default' => 1,
),
'version' =>
array (
'description' => 'The version of the provider\'s data.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
'default' => 0,
),
'title' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'description' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'duration' =>
array (
'description' => 'Store the duration of a video in seconds.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
'default' => 0,
),
),
'widget' =>
array (
'video_width' => '425',
'video_height' => '350',
'video_autoplay' => 0,
'preview_width' => '425',
'preview_height' => '350',
'preview_autoplay' => 0,
'thumbnail_width' => '120',
'thumbnail_height' => '90',
'thumbnail_default_path' => '',
'thumbnail_link_title' => 'See video',
'meta_fields' =>
array (
'title' => 0,
'description' => 0,
),
'providers' =>
array (
'vzaar' => 'vzaar',
),
'default_value' =>
array (
0 =>
array (
'embed' => '',
'value' => '',
),
),
'default_value_php' => NULL,
'label' => 'Vzaar Video URL',
'weight' => '28',
'description' => '',
'type' => 'emvideo_textfields',
'module' => 'emvideo',
),
);

// Add the new content type to drupal
content_field_instance_create($vzaar_media_field);

}

Advertisements
h1

Rename files in directory with some regex magiks

April 12, 2011

When using drupal I often want to clone a module as a starting point for a new module. I then have to rename all the files in the directory to the new structure. This can be painful if there is more than one file. All files in drupal follow the file naming convention of :
modulename.type
So the following bash script will niftily go through a directory renaming all the files with module1 and replace with module2…


for f in *; do mv $f `echo $f | sed 's/module1\(.*\)/module2\1/g'`; done

h1

Print windows line endings with PHP

March 11, 2011

To print windows line endings when writing to a file using php you can use \r\n escape characters. e.g.

print "some line \r\n";

For unix/linux style line endings you just use \n;

h1

Find and replace with sed and find

March 7, 2011

To find all instances of a string in a file you can use grep like so

grep -n findstring somefile.txt

To replace all instances of a string with another string in a file you can use sed like so

sed -i 's/find/replace/g' somefile.txt

To find all instances of a string in all files in a directory you can use grep like so

grep -n findstring /some/dir/*

To replace all instances of a string in all files in a directory you can combine sed and find like so

find /some/dir -type f -exec sed -i 's/find/replace/g' {} \;

-n on grep makes it print off the line numbers of where it found the search term

-i on sed means inplace and means it will do the replacement in that file and save it. You can do -iold if you like and every altered file will then be backed up to .old first

-type on find means only regular files are returned.

-exec on find means that the command following will be executed for each found file. The {} items puts the filename into the command. All args after -exec are assumed to be arguments of the command until a semi-colon is found. The semi colon has to be escaped otherwise it will be seen as a command line end.

Simples!

h1

Speeding up Eclipse

January 25, 2011

Eclipse can get a bit sluggish, especially when you’ve got the Scala and Maven plugins installed into it. I found the following website with some useful improvements you can make to the eclipse.ini file to speed it up. This works for Windows and Linux systems and will probably work with Mac as well. It give eclipse more memory so if you are running a dev machine with 4GB of RAM then this should work fine. The eclipse.ini file can be found in the root of the eclipse application folder.

The vmargs parameter as shipped uses these heap settings (at the bottom of the eclispe.ini file):

-Xms40m
-Xmx364m

It needs to specify a larger heap and more stack.

-Xss8m
-Xms800m
-Xmx2048m
-XX:NewSize=400m
-server
-XX:+DoEscapeAnalysis
-XX:+UseConcMarkSweepGC

Adjust the max heap size (“mx”) to be sure that it fits in real memory. “ms” should be twice the “NewSize”.

Ref: http://projectfortress.sun.com/Projects/Community/wiki/Eclipse

h1

Putting properties in the web xml file

January 25, 2011

If you are building a web app in Java or Scala, you might want to feed in a variable from outside which describes something special about the particular environment the web app is running on.  On a normal application you might do this from the command line with a command line argument.  In a web app, you put the variables in the web.xml file. In a maven built project this can be found in /src/main/java/webapp/WEB-INF/web.xml

The XML for the variable looks like this …

<env-entry>
<env-entry-name>PROPS_FOLDER</env-entry-name>
<env-entry-value>/opt/webapp/properties</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

In your code, the property can be accessed by any class which extends HttpServlet. The following imports are therefore required:

import javax.servlet.http.HttpServlet;
import javax.naming.Context;
import javax.naming.InitialContext;

The property can then be accessed in your Java code like this:

Context env = (Context) (new InitialContext().lookup("java:comp/env"));
String PROPS_FOLDER = (String) env.lookup("PROPS_FOLDER");

And in Scala like this:

val env:Context = (new InitialContext().lookup("java:comp/env")).asInstanceOf[Context];
val PROPS_FOLDER:String = env.lookup("PROPS_FOLDER").asInstanceOf[String];
h1

Making a field unique after creating a table

December 15, 2010

The MySQL browser on Linux doesn’t appear to let you add unique column indexes to a table in retrospect after the table has been created. There are mysql commands though which allow this:

ALTER TABLE tablename ADD UNIQUE name_of_index (col1,col2...);

Statement to make unique columns without altering table:

CREATE UNIQUE INDEX name_of_index ON tablename (col1,col2,col3...);