Drupal Views Pager with Different Default Page

Posted by ryochan7 on January 18th, 2012  •  View Comments  • 
Tags: drupal

When working with Drupal Views recently, I wanted to make the Views pager behave like the Related Videos block of the Videos section on this site. In other words, I wanted the block to display the page that the current content is on by default as opposed to showing the first page of content. The primary focus of the site I am working on is video focused so using the Videos section for reference made sense.

I couldn't find a solution online for my problem so I figured that I should post my findings. Getting the page that the content resided compared to other videos was not too difficult and it just involved a simple hook implementation. The hook that was implemented was the hook_views_pre_build ().

/**
 * Implements hook_views_pre_build ()
 */
 function hfcustom_views_pre_build(&$view) {
     if ($view->name == "related_playthrough_vids") {
         if (!isset ($_GET["page"]) || !is_numeric ($_GET["page"])) {
             $node = node_load (arg (1));
             if ($node->type == "playthrough_video") {
                 $query = db_select ('node', 'n')->condition ("n.type", "playthrough_video", "=")
                   ->condition ("n.nid", arg (1), "<");
                 $query->join ("field_data_field_playthrough", "play", "n.type = play.bundle AND n.nid = play.entity_id");

                 $query = $query->condition ("play.field_playthrough_nid", $node->field_playthrough["und"][0]["nid"])->countQuery ();

                 $num_rows = $query->execute ()->fetchField ();
                 $page = floor ($num_rows / 5);
                 //$remain = $num_rows % 5;
                 $view->current_page = $page;
                 $view->items_per_page = 5;
             }
         }
     }
 }

Once the proper view has been detected, the function checks if a page number has been specified as a GET variable and then tries to find the appropriate page number if one has not been provided. The nid of the current node is taken from the URL and then the node is loaded. Then a count query is used to find the number of videos stored before the current video. The placement in the pager and specifying the number of items in the list is performed at the end of the function; the value for number of items specified in the Views UI is not accessible at this point so it must be specified here as well.

Now that a proper default page has been determined, the next issue that had to be fixed was that the page number has to be explicitly specified for the first page; Drupal will usually omit the page component for the first page but that behavior will cause issues when using a different default page. It took too long to figure out but I eventually came up with a solution. Firstly, more views hooks had to be implemented. hook_views_pre_render () had to be implemented as well as hook_views_post_render () in order to undo my change in.

/**
 * Implements hook_views_pre_render ()
 */
function hfcustom_views_pre_render (&$view) {
    if ($view->name == "related_playthrough_vids") {
        global $in_playthrough_vid;
        $in_playthrough_vid = true;
    }
}

/**
 * Implements hook_views_post_render ()
 */
function hfcustom_views_post_render (&$view, &$output, &$cache) {
    global $in_playthrough_vid;
    if ($view->name == "related_playthrough_vids") {
        $in_playthrough_vid = false;
    }
}

Even though I don't like using global variables, this was one case where using a global variable was necessary. $in_playthrough_vid designates that the related_playthrough_vids block is being rendered. The variable will later be used in a preprocess template function.

function hellfirecomms_preprocess_pager_first (&$variables) {
    global $in_playthrough_vid;
    global $pager_page_array;
    $element = $variables["element"];

    // Only do this on first page
    if ($in_playthrough_vid && $pager_page_array[$element] == 1) {
        $variables["parameters"] = array ("page" => 0);
    }
}

The relevant function for the upcoming pager is the theme_pager_first function. As opposed to overridding the theme_pager_first function, a template preprocess function can be used to change the input variables being passed to the theme_pager_first function. In the previous code, the function will check that the related_playthrough_vids block is being rendered and that the first page link should be rendered. If so, the page number for the first page must be given. The way to do this is to add the page number to the parameters variable.

With this code in place, a page number will be passed for the first page which will work with the hfcustom_views_pre_build function mentioned earlier. Without the page number, the new default page would have loaded again and the first real page would never be accessible.

This is the extent of my pager customization adventure. It is good that Drupal is so flexible.

ryochan7.com open sourced

Posted by ryochan7 on April 19th, 2011  •  View Comments  • 
Tags: blog, projects, github, ryochan7

Over the past day or so, I have been making changes to how this web site is deployed. This web site now utilizes virtualenv. Using virtualenv along with pip has allowed for a much easier transition between development and production setups. My development and productions Python environments can now be identical without much of a hassle and different environments can use different versions of similar Python packages; being able to specify dependencies and install them from pypi using pip is also a great feature. This method of deploying Django projects has become fairly common and working with virtualenv has shown me why it is becoming popular.

Also, I have cleaned up the code for this web site a little bit. To help ease pushing updates to the Webfaction servers and as a means of showing what components make up this web site, the code for this web site is now on GitHub at https://github.com/Ryochan7/asylum.

YouTubed-2x 2011.03.26 release

Posted by ryochan7 on April 15th, 2011  •  View Comments  • 
Tags: projects, youtubed-2x

If you have been paying attention to my GitHub profile, you would have seen that I have been making improvements to my old programs. YouTubed-2x is an application that I wanted to fix up using the knowledge that I have gained over the past couple of months. YouTubed-2x has been a decent flash video downloader but there have been issues that have been a part of the program for too long.

I am releasing a new version of the main archive because there are still some downloads of the older archives. One thing that must be mentioned is that there is no pre-built Windows version. Running this application on Windows would require an installation of Python, the PyGTK all-in-one bundle, and pywin32 extensions; I cannot get py2exe to cooperate with my new setup. Some of the improvements in this version are more useful on the development front but there are a few changes that users would probably like.

  • Test suite rewritten. Now each parser test is independent and can be tested separately. It is now much easier to test which parsers are broken using only one test run.
  • Logger starting to be used in the application.
  • The setup.py file has been altered to make it much easier to distribute the application.
  • gsignals are now used in the videothreadmanager to help ease updating the GUI.
  • Tweak for finding an appropriate My Videos folder in Windows.
  • GUI no longer locks during video adding. Performing tasks on other video threads is now possible while one thread is parsing a video page.
  • Download speed is now shown in taskbar.
  • Fixed a long standing issue with locking which would not allow GUI elements to become sensitive after parsing a video page. This issue has annoyed me for over a year but I could not figure out what the problem was. A lock release was being circumvented when canceling a thread which would result in the starting of another thread to no longer be possible. Fixing this issue has shown me the importance of a debugger.
  • YouTube and GiantBomb parsers fixed. This was primarily done in order to test the application.

This application will still not be under major development unless people want continued development of YouTubed-2x. I am happy with the new version and it works very well. Please post comments if you still have an interest in YouTubed-2x.

Screenshot:

YouTubed-2x 2011.03.26

Download: http://www.ryochan7.com/files/projects/youtubed2x/YouTubed-2x-2011.03.26.tar.gz

Easier Development Path

Posted by ryochan7 on April 6th, 2011  •  View Comments  • 
Tags: vim, winpdb, python, netbeans

Over the past few months, I have tried to find ways to be more productive during my coding sessions. Usually, trying to be more productive would just mean writing code and learning something new. However, after my major project during my last semester of school, I found out the hard way that the old way of doings things was not working out well.

The major problem at the time became that I did not have a good tool set in order to make coding easier. My main editor for programming was simple Gedit. I had started using a more minimalist route years ago during my experience writing C++ code with Visual Studio. I didn't not want to be tied down to a tool, especially one that was really buggy. I started writing my coding assignments using Gedit and then compiling the code with g++; I did use Anjuta sometimes but not that often. However, managing larger code bases using that same approach does not work.

Refactoring code was a mess just using Gedit and no autocompletion or documenation look up forced me to search for documentation on Google a lot. Nowadays, I am using GVim for making smaller edits while I am still learning how to use it and NetBeans for project management; PIDA is also a pretty good Vim option for Python development. Refactoring code is so much easier with either program and both programs can perform autocompletion as well as documentation look up and easy navigation to declarations of functions.

Also, I finally have started using tools and performing practices that I should be better acquainted with by now. I have started writing tests as a part of my standard coding practices. Tests are now at least done for the main models of an application. Learning more about testing has really helped with my old YouTubed-2x application since now I have a lot easier way to test for broken parsers. Although, I need to learn more about writing meaningful tests as well as documentation.

A type of tool that I really should have used sometime during my schooling was a debugger. However, I made it through undergrad without ever using a debugger on a project. My debugger of choice was print. To fix this problem, I had forced myself to learn how to use a debugger and my tests have shown me just how important debugging code is and how much easier using a debugger is as opposed to writing text to stdout/stderr. Using a debugger allowed me to fix a couple of long standing bugs in YouTubed-2x that have existed for well over a year. My favorite Python debugger is Winpdb; the debugger in Eric IDE is also good but I have found Eric to be unstable on my system.

Besides learning a lot, what I have been doing has already helped me fix issues that I have had in my projects. I feel like I can approach problems much more easily now and develop solutions more quickly. I feel like I have leveled up over the past few months. Hopefully, these experiments will help in the process of getting a job.

Now on Github

Posted by ryochan7 on February 27th, 2011  •  View Comments  • 
Tags: projects, github

Long time no see. I am writing this post to mention that I have started using GitHub and I have been comitting a lot of my projects. So far, I have added 5 repositories although one was just a coding challenge for a prospective job. YouTubed-2x, StepMania Unlock Code Generator (Python and Java) and also Pizza Py Party are now on GitHub. Along with learning how to use Git, I have also starting using PyQt and I have converted StepMania Unlock Code Generator over to using Qt. The Qt version is in the master branch of the repository and the GTK version is in its own branch (gtkarchive).

https://github.com/Ryochan7

Other than this, I have not done much over the past couple of months. The only other thing is that I have started using Vim as my primary editor as opposed to using Gedit. That is about all I have to mention.

Pulled from the Archives - StepMania Unlock Java

Posted by ryochan7 on December 22nd, 2010  •  View Comments  • 
Tags: sucg, projects

In a previous post, I mentioned an experiment that I did with NetBeans. The experiment was writing a version of StepMania Unlock Code Generator in Java using Swing for the interface. I finally found a copy of it again and I figure that I should put it up here. It should be equivalent to the 0.6 Python release in functionality.

http://www.ryochan7.com/files/projects/sucg/sucg_java.zip

StepMania Unlock Code Generator Java

Update December 20 2010

Posted by ryochan7 on December 20th, 2010  •  View Comments  • 
Tags: blog

School is over. For right now, I have a lot of free time but I don't know what to do.

Quick Post June 2010

Posted by ryochan7 on June 6th, 2010  •  View Comments  • 
Tags: blog

I am not dead. I had been busy with school and now I doing some stuff during my break. First off, even though I am 26 years old, I just got a drivers license a month ago. I still have to get a car but I am looking. One other big task to tackle is getting a job.

As for software projects, I am currently helping with the development of the best Sonic the Hedgehog web site ever: Find the Computer Room. Progress is coming along in testing but I don't know when anything will be placed on the main site. Pizza Py Party is now back in development for the time being thanks to the contributions of reid.kleckner. Pizza Py Party is now on Google Code @ http://code.google.com/p/pizza-py-party/.

As for this blog, I won't post often as I usually don't have anything significant to post. And I'm out.

YouTubed-2x 2010.01.22

Posted by ryochan7 on January 22nd, 2010  •  View Comments  • 
Tags: projects, youtubed-2x
Updated on January 25th, 2010

It has been a while since I put out a release. I have done some experimenting but this version is similar to the last one. Updates

  • Translation support in progress although I doubt this program will every be translated to another language
  • Parsers slightly refactored
  • Broken parsers fixed (can't remember which were broken)
  • Queued items that have not been started are saved when saving a session now. Only items that had been started but were either paused, cancelled or finished would appear in the previous release
  • Made a Windows version for this release
  • Likely more but I can't remember

Downloads:

Linux: http://www.ryochan7.com/files/projects/youtubed2x/youtubed-2x_2010-01-22.tar.gz Windows: http://www.ryochan7.com/files/projects/youtubed2x/youtubed2x-win_2010-01-22.zip Archive: http://www.ryochan7.com/files/projects/youtubed2x/youtubed-2x_archive_2010-01-22.tar.gz

Screenshot:
YouTubed-2x 2010.01.22

New Coding Adventures 2010

Posted by ryochan7 on January 3rd, 2010  •  View Comments  • 
Tags: coding, sucg

Over the past couple of weeks, I have done some coding experiments that I have wanted to do for a while. I first started by brushing up on my Java more. My major goal was to finally make an application that used Swing since I had not directly worked with Swing before; I have worked on one group project that implemented Swing but I did not work on making the UI. Two issues that I have seen with Swing projects at school in the past that people seem to overlook is regarding widgets not being resizeable and being able to resize an application window to the point where you can't see the widgets since no minimum window size had been set. I had not seen one Swing project at school take either of these issues into consideration so I wanted to find a way to avoid both problems.

I ended up making a version of StepMania Unlock Code Generator using Java. Besides learning Swing, I finally learned about many of the classes used for file I/O, anonymous inner classes, sorting, and a little bit about internationalization. NetBeans makes it easy to define text in resource files in order to implement internationalization; the project is my first project that is technically translatable. The project is also my first project where I decided to use the use-case controller approach for keeping business logic organized. I feel like I learned quite a bit about Java that I didn't know in a short period of time.

StepMania Unlock Code Generator in Java

After working on that, I wanted to mess around with making applications for the GP2X. I started by getting the Open2x toolchain and learning how to use it. Then, I compiled the source code for Onscripter and played around with Narcissu; I used the data files from the current GP2X port. This wasn't the best way to learn about GP2X development as it took a long time to figure out how Onscripter worked and learning how much logic was in the game script. I made a few modifications but I gave up on messing with the program within a few days. The current GP2X port of Narcissu has a few bugs that I wanted to try to fix; a couple of the bugs are the game going back to the title screen after exiting the configuration screen and the game crashing during a certain portion early in the game that I can't remember at the moment.

After my failed efforts with Onscripter, I found a simple Hello World example that used SDL and I started to take things slowly. Since I can't get USB Networking support working on my GP2X anymore, I got tired of copying executables to my SD card and then running applications on my GP2X. I figured that it would be better for me to learn SDL on the PC first so that I could have an application that worked on both the PC and the GP2X which would make things easier to test. I started with simple stuff like displaying images and text to a screen. The most that I have done so far is making a Pong clone. For learning resources, I have looked at the source code of many projects and I have read the tutorials on the Lazy Foo' website. Even though I haven't made much progress yet, I am glad that I finally have a reason to program in C++ again.

Now, I am kind of discouraged so I haven't done any coding over the last week. Instead, I have been playing a lot of Sonic Unleashed and trying to get as many achievement points for that game as I can. Sonic Unleashed is the first game that I have ever focused on getting achievements for and it is sometimes really tedious to do some of the tasks required to get some of the achievements.

That has been almost everything that I have been up to during my break from school. At least I am having fun for a change.

More Blog Posts