Archive for Drupal

How Do I Make a Drupal “repeater field” with Field Collections (D7)

Up until now I’ve been mainly working in WordPress, mainly because of my love for ACF and their abilities to have repeating groups of fields. And I finally found a resource that helped me achieve the same user content manageability using Drupal. First off, you’ll need to install the Entity and the Field Collection modules by downlowding the files from the above links and putting the folders in the /sites/all/modules folder. After that dont forget to active them both in the admin at site.com/admin/modules

So now we can start setting ourselves up.

<?php
/**
* This function takes a field collection field and returns an object array that contains all of the field
* collection entity's fields. You can then use it the same way you would a node object. For example:
* $loaded_field_collection =  load_collection($node->your_field_collection);
* print 'check this ' . $loaded_field_collection->your_collected_field['und'][0]['value'] . ' value, sucka!';
*/
function load_collection($entity_uri) {
  $loaded_field_collection = entity_load('field_collection_item', array(0 => $entity_uri));
  foreach ($loaded_field_collection as $loaded_field_collection_object ) {
    return $loaded_field_collection_object;
  }
}
?>

And clear the cache at site.com/admin/config/development/performance
In fact, i just wrote a blog about how many times clearing the cache ended up being my problem.
So now you are ready to tackle the node.tpl.php file.  This works for other any of the other template files too, like node–news.tpl.php

<?php
/**
*
* For this example my content type was configured like this:
*
*  node
*   - top_level_collection
*     - second_level_collection
*        - text_1
*        - image_1
*        - third_level_collection
*         - text_2
*         - image_2
*     - second_level_collection_2
*        - text_3
*        - list_1
*        - image_3
*/
 
// check if field collection is not empty. If content exists, do some stuff.
if (!empty($node->top_level_collection['und'])) {
  //Your custom wrapper goes here
  print '<div class="collectin-it">';
 
  foreach ($node->top_level_collection['und'] as $a_top_level_collection) {
    //Use the load_collection() function to load your top level field collection's entity into an object.
    $top_level = load_collection($a_top_level_collection['value']);
    //You can use Devel's dsm() to view this object in the Devel tab of your node. Comment out or delete when everything's working well.
    dsm($top_level);
 
    //if your top_level_collection contains any fields, you can print them here using the $top_level object. Example: print $top_level->your_field['und'][0]['value'];
 
    //Use the load_collection() function to load your second level field collection entity object from the field in the top level field collection's entity object. All notes from the preceeding level apply.
    foreach ($top_level->second_level_collection['und'] as $a_second_level_collection) {
      $second_level = load_collection($a_second_level_collection['value']);
      dsm($second_level);
      foreach ($second_level->text_1['und'] as $a_text_1) {
        print '<p>' . $a_text_1['safe_value'] . '</p>';
      }
      foreach ($second_level->image_1['und'] as $a_image_1) {
        $var = array(
          'item' => $a_image_1,
          'image_style' => 'large',
          'path' => ''
        );
        print theme_image_formatter($var);
      }
 
      //Just like above, you can load the next level from the field in this level's field collection entity object.
      foreach ($second_level->third_level_collection['und'] as $a_third_level_collection) {
        $third_level = load_collection($a_third_level_collection['value']);
        dsm($third_level);
        foreach ($third_level->text_2['und'] as $a_text_2) {
          print '<p>' . $a_text_2['safe_value'] . '</p>';
        }
        foreach ($third_level->image_2['und'] as $a_image_2) {
          $var = array(
            'item' => $a_image_2,
            'image_style' => 'medium',
            'path' => ''
          );
          print theme_image_formatter($var);
        }
      }
    }
    //and finally, for the above mentioned case, you can go back to the $top_level object and load that second level field collection #2.
    foreach ($top_level->second_level_collection_2['und'] as $a_second_level_collection_2) {
      $second_level_2 = load_collection($a_second_level_collection_2['value']);
      dsm($second_level_2);
      foreach ($second_level_2->text_3['und'] as $a_text_3) {
        print '<p>' . $a_text_3['safe_value'] . '</p>';
      }
      foreach ($second_level_2->check_1['und'] as $a_check_1) {
        print '<p>Checklist value is: ' . $a_check_1['value'] . '</p>';
      }
      foreach ($second_level_2->image_3['und'] as $a_image_3) {
        $var = array(
          'item' => $a_image_2,
          'image_style' => 'thumbnail',
          'path' => ''
        );
        print theme_image_formatter($var);
      }
    }
  }
  //close your custom wrapper.
  print '</div>' . "\n";
}
?>

And that’s all you do! Thank you Rob W for all your contributions to Drupal and teaching me the ways of the Field Collections
Source

node–contenttype.tpl.php not overriding

I’ve worked in Drupal once before and I had forgotten the most important thing to know…. manually clearing your site’s cache.
Basically this is a step you need to take when making a new template. In fact, if anything looks off with your drupal site, it could quite possible be the cache. (This is for people that set it up correctly, if you haven’t then your problem is probably something else)

So after you make the “node–contenttype.tpl.php” file in your templates folder, do the following steps:
1) go to “Configuration”
2) go to “Performance” (in the Development category)
3) click the “clear all caches” button

This solved my problem, hopefully this helped you too
Source

Drupal vs. WordPress

I’ve been working on a Drupal site this past week or two. I had done absolutely zero with Drupal before that and had to essentially power learn the CMS just for this project. As this was straying away from my comfort zone, WordPress, I started keeping in mind comparisons to WordPress. I have to admit I am still a noob to Drupal and don’t know all the tricks so the below opinions should be taken with my situation in mind.

Drupal
Pros:

Integrated PHP into Content - WordPress has different plugins that let you use PHP in posts but Drupal is far and away more efficient at this.

Awesome User SystemThe user system on Drupal works really well and very extendable with really advanced permissions.

Integrated AdministrationContent administration is built right into the main theme. WordPress has 2 different sides, the site and the admin backend.

BlocksThis is more a neutral than a pro. Blocks can get annoying to edit here and there when WordPress sidebar widgets are much easier. This is more like Joomla, in that you set different sets of content into specific areas of the page called blocks.

Cons:

Manual Module InstallWordPress lets you search and automatically install plugins right from the backend

Complicated ThemingCreating themes was very complicated in Drupal, at least for me. There are a few strange things when you are new to WordPress, The Loop for example. But I had a much harder time with drupal.

Lack of customization
This is the most frustrating thing for me. I like what modules do for me but I need to style the output I get back. For most of my issues I had to go in and edit functions inside the module files and even the core files itself. That is some really advanced stuff for a supposed user-friendly Content Management System. In WordPress, I RARELY had to edit plugins since they offer enough css to style it or plenty of other options. If I did you could edit it in the backend or in dreamweaver in glorious COLOR. Drupal uses a .module extension which you have to add to Dreamweaver to tell it it is safe to even open it. Plus there is no color scheme to a .module extension even though its all php. Thanks for the hassle. Also, the WordPress plugin/hook system is much easier to understand then the Drupal API layering.

Debugging
This might only be my case but I ran into multiple problems where what was showing is not even close to the logic used. What my source html and CSS code was listing contradicted what my eyes say. I have just noticed a weak error layer with Drupal.

Documentation/VersionsDrupal comes in a few different versions but the problem is many have drastic changes. How something is coded is changed in each version. You can’t say you know Drupal unless you know how to make a site in each version, 4, 5, AND 6. WordPress has compatibility issues between versions, mainly in plugins, but change in actual core functions is VERY minimal in WP. Drupal’s multiple version also resonate a whole other problem. Documentation is HORRIBLE. Drupal’s home site will tell you how to do different functions in each version but not alot of example or variations or implementations, and most of all a huge lack of help when trying to find out what arguments a function takes and what they do. Worse yet, to find solutions for your issues on any other site is a nightmare since how you handle something in Drupal 5 could be completely different than 6.

Now if I got a little more comfortable in Drupal AND I had to build a huge community-type site, I would consider Drupal. Just the user system would save a bunch of time that way. But for any other type of site I can’t see any reason not to use WordPress.

I guess this post wasn’t a very close argument in my mind. A more close comparison might have been Joomla vs. Drupal but I’m not sure if I could make a verdict on that. But the point of this post is…after a week or so of pure Drupal grinding all I could do was think about WordPress as if she was the girl that got away.