Friday, June 21, 2013

Treehouse learn to make ios apps, android apps, web design and programming.

Searching more for a way to pick up my design skills being a programmer and all I came across this treehouse site ( link is below )

Just started today so I won't have a deep review but here's what I like and dislike:
  • I like the why it is set up, building from the scratch up. Other sites I cant really find a start point and I and up not looking into the trainings. 
  • You can take a quiz or code challenge along the way. first of all for me it's checking if I already own those skills of if I should check in to this in a few seconds.
  • Second, you earn badges which may be a bit childish but I think it's fun. Also potential employers can see the badges you earned. And last it gives you hands on practice while you try out new stuff you just learned.
  • As you progress you can view on your dashboard where you are in the courses. So you can easily pick up any time where you left off.
  • There is a litte bit of humor in the video's which I think takes the edge of some things and makes it a nicer learning experience.
  • Now what I don't like I have to think about... Maybe it could have been a bit less childish ? If I come up with some thinks I'll add the likes and dislikes later to this post
The prices are ok I have to see if this is really for me but looked at a few and I must say I love it so far!
It starts from 25$ a month or 250$ if you pay annually ( 2 month discount ) for the silver account.
The gold account is a bit more pricier but I think you will have enough with the silver account.

Also you can earn 20% discount for every subscriber you refer, which is what I'm doing right now ;)
And I would appreciate It if you used my subscriber link. I'll give you the 20% discount I will be getting for the first year back if you contact me.
But other than that I still think it's a good think. Check out the free trial video's and see why I like it.

So check it out the link is in this join me on treehouse image below:

Create one time login link from the command line using drush no need to reset your password

I don't use this command as much but when I need it I always forget what this string of code was.

So I'll post it on my blog :)

The magic:
drush uli

Or if your targeting a specific user:
drush uli username

Using drush you can create a one time login link for a user or admin.
This is especially usefull if you want to login to the site without changing the password.
For example if you have a client with a drupal site that doesn't know the administrators pass or doesn't want to change it because you need it.
This can also be used to change the password if you don't know where the password reset link would be send or don't have access to the email address ( anymore )

Wednesday, June 19, 2013

Creating a link field programmatically for a profile2 user entity and deleting it upon disable or uninstall

In this example I have a profile2 profile called "werkgever"

The field I'm creating is a field used for that profile to input a website link.

required modules are
link
profile2

My module name is more_werkgever_fields

Using hook_enable I create the field:
/**
 * Implementation of hook_enable().
 */
function more_werkgever_fields_enable() {
  // Check if our field is not already created and if not create the field.
  if (!field_info_field('field_website')) {
    $field = array(
        'field_name' => 'field_website',
        'type' => 'link_field', // text
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'field_website',
      'entity_type' => 'profile2',
      'label' => 'Website',
      'bundle' => 'werkgever',
      'required' => FALSE, // default: FALSE
      'settings' => array(
         // Here you inform either or not you want this field showing up on the registration form.
        'user_register_form' => 1,
      ),
      // 'widget' => array(
      //   'type' => 'link',
      // ),
    );
    field_create_instance($instance);
  }
}

This needs to be put in your modulename.install file

Also in the .install file you can implement a hook_disable or hook_uninstall
hook_disable is called when you disable the module. So this is handy if you are in a developing stage.
Just disable de module and your field should be gone.
An implementation of hook_disable:
/**
 * Implements hook_disable
 */
function more_werkgever_fields_disable(){
  // removes field when module is disabled
  field_delete_field('field_website');
}
As said this goes into your module.install file as well
Finally there is the Implementation of hook_uninstall:
/**
 * Implements hook_uninstall
 */
function more_werkgever_fields_uninstall(){
  field_delete_field('field_website'); // removes field from the system
}
The uninstall hook is only called upon uninstalling the module. I recommend putting your delete field in here. Or don't delete your field. Depending if your your module can be reused by others and maybe causing problems if that field is to be deleted. In my case I don't want it to clutter my database so I put it in my uninstall hook.

Monday, April 22, 2013

Drupal 7 Create a block that's only visible on specific pages programmatically.

A better view of this post can be viewed here on stackexchange

1) Create a new module or copy an existing one and rename it.

2) Enable PHP filter module ( included in core )

3) Create a block using the hook_block_info() hook.
For example:

 /**
 * Implements hook_block_info().
 */
function mymodule_block_info(){
  $blocks['mymodule_block_name'] = array(
    'info' => t('Block description'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
    'region' => 'sidebar_first', // assign block region
    'status' => 1, // enable the block
    'visibility' => BLOCK_VISIBILITY_PHP, // set visibility to use the result of pages code
    'pages' => '',
  );
  return $blocks;
}
TIP: create a block using the block UI after enabling the PHP filter module so you can test your 'pages' PHP code before putting it in the module and prevent having to disable, uninstall and enable your module every time you change your php code.

4) Create the content of the block using hook_block_view():

/**
 * Implements hook_block_view
 */
function vacancies_block_view($delta = '') {
  switch ($delta) {
    case 'mymodule_block_name':
      $block['subject'] = 'Block title';
      $block['content'] = 'Hello world!';  
    break;
  }
  return $block; // dont forget to the return the $block!
}

5) upload your code and enable the module
Or if you used an existing module disable, uninstall, enable the module

6) clear cache and check the result

Tuesday, October 30, 2012

Adding menu items to an drupal 7 install profile

using the .install file ( located here: <websitedir>/profiles/<profilename>/<profilename>.install )

You can add menu items by extending the array like in the standard install profile:

adding this will put the block main-menu in the menu_bar region:

array(
      'module' => 'system',
      'delta' => 'main-menu',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'menu_bar',
      'pages' => '',
      'cache' => -1,
    ),
The trick was here setting the module to 'system' instead of 'menu' otherwise errors will show up after install. 
You can define the theme you want to apply this for or don't set it to apply on all themes. Since this is a system default location you should be fine not setting the theme.

Then let the following query run ( just like the standard.install file )


 $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($values as $record) {
    $query->values($record);
  }
  $query->execute();
Then creation the menu items like so:


// create menu
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
  );
 menu_link_save($item);


Optional you could do a menu_rebuild but I didn't see a difference while installing.
maybe this is only for modules so the menu gets 'cleared' after enabling a module.
since the whole site is just build it might not be necessary doing this in a profile .install file


Update the menu router information.
menu_rebuild();


Monday, October 29, 2012

Setting a different default theme by default in an install profile

For an install profile you can look at the profiles that are included in the drupal install.
They can be found in sitename/profiles
As in an standard Drupal 7 instal there are 2 profiles 'minimal' and 'standard'.

The profiles are somewhat like modules and also work like that.

The profilname.info file can be used to define dependencies and thus enabling the defined modules when this profile is chosen.
again look at the standard.info file for an example.

In the .profile file you can alter the form that is used upon install and predefine values for fields.

but to set an other default theme you need the .install file.

In the .install file you place an hook_install()

at the end of your profilename_install() hook you place the following code:
profilename_install() {
// some other code

  db_update('system')
    ->fields(array('status' => 1))
    ->condition('type', 'theme')
    ->condition('name', 'yourthemename')
    ->execute();
  variable_set('theme_default', 'yourthemename');
}
Just make sure that yourthemename is available in your install directory.


Setting the theme settings.

The next step is setting the theme variables but I haven't figured that out so I will update this post when I get there...

I think you can set the values in the 'system' table WHERE `name` = 'themename'
Best way to do this is click the theme to the point you like it, then go to the database and copy the values of the according `info` field.

Friday, October 26, 2012

adding a city autotagging field to an user by creating a custom drupal 7 module

This posts tells you about how to programmatically create a field, instance, taxonomy library and create an autotagging widget in the form with it.

First in your module.install file create a hook_enable() function.
In the final version this would be hook_install() but while testing the module I find it easier to use hook enable so you can do: drush en mymodule -y and drush dis my module -y to enable and disable the module.

My module is called popover so that's the name I'll be using next.


/**
 * Implements hook_enable
 * runs every time this module is enabled, to run only at install use hook_install
 * Once the module has been enabled ( even only once ) hook_install won't be called anymore.
 */
function popover_enable() {
        // starting with the taxonomy library ( vocabulary ) first:

$vocabulary = (object) array(
'name' => 'City',
'description' => 'Pick your city :)',
'machine_name' => 'city',
);
taxonomy_vocabulary_save($vocabulary);
$terms = array(
'Amsterdam',
'Rotterdam',
'Rosmalen',
'Driebergen',
'Zeist',
'Utrecht',
);
        // I've created a custom function to save multiple terms to one vocabulary:
_popover_taxonomy_terms_save($terms,$vocabulary->vid);

        // building the field
$field = array(
'field_name' => 'field_' .$vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($field);

        // connecting the user to the field I've just created
$instance = array(
'field_name' => 'field_' . $vocabulary->machine_name,
'entity_type' => 'user',
'label' => $vocabulary->name,
'bundle' => 'user',
'required' => TRUE,
'widget' => array(
'type' => 'taxonomy_autocomplete',
'weight' => -4,
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
'teaser' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
),
);
        // another custom field creator function
popover_field_create_update_instance($instance);
}



And the function I'm calling from the code above:

/**
 * save multiple terms to one taxonomy Vocabulary
 * @param  array $terms array of term names
 * @param  int   $vid   Vocabulary id
 */
function _popover_taxonomy_terms_save($terms,$vid) {
foreach($terms as $term) {
taxonomy_term_save(
(object) array(
'name' => $term,
'vid' => $vid,
)
);
}
}

/**
 * field creater helper function checks if field already is in this bundle if so do update instead of create
 */
function popover_field_create_update_instance($instance) {
if( field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']) == NULL) {
return field_create_instance($instance);
}
else{
return field_update_instance($instance);
}
}



Now because the instance is in the user bundle the field should now be showing up on the user edit form.

To delete the instance, field and vocabulary upon uninstall of the module. I've written the following:


/**
 * Implements hook_disable()
 * TODO: needs to be hook_uninstall in the future but again for testing using the hook_disable
 */
function popover_disable() {
// create array with instances/fields/vocabularies to cleanup
  $cleanups = array(
  'city',
  // you can add more fields here...
  );
  foreach($cleanups as $name) {
  _popover_cleanup_instance_field_vocabulary($name);
  }

  // force field purge ( so it is really deleted without waiting for cron )
  // I think I would remove this in the final version as well since your not going to uninstall a
  // module and then reinstall it again immediately after. besides opening the module page 
  // and then the unistall page calls cron if I remember correctly but not sure about that... anyway:
field_purge_batch(5000); // not sure if this is a to high number...
}

/**
 * cleanup helper function
 * @param  String $name machine_name of the vocabulary ( 'field_' is added for the field/instance)
 */
function _popover_cleanup_instance_field_vocabulary($name) {
$instance = field_info_instance('user','field_' . $name,'user');
// removing instance of field_$name in the user bundle
field_delete_instance($instance, true);
// deleting field
field_delete_field('field_' . $name);

// delete city vocabulary
$vid = db_select('taxonomy_vocabulary', 'v')
    ->fields('v', array('vid'))
    ->condition("v.machine_name", $name)
    ->execute()
 ->fetchField();
  taxonomy_vocabulary_delete($vid);
}

You can see the whole module on my github But I'm still working on it so don't expect it to work 100%