Hướng dẫn làm việc Arrays và Objects trong Drupal with print_r() and dpm()

Hướng dẫn làm việc Arrays và Objects trong Drupal with print_r() and dpm()

When customizing things in Drupal, whether we like it or not, there often comes the need to dive in and alter or override things. Because of Drupal's API, we're given a lot of wonderful hooks and theme functions to take advantage of without touching Drupal core. Instead we can modify most things the way we want within our own custom module or theme, without ever having to worry about our changes getting overwritten when doing an update to our web site.

Great! There's plenty of tutorials, blog posts, forum posts and issues both on drupal.org and throughout the interwebs on how to work with data in order to bend it to our wills, but it's not always clear on how exactly to work with the data in the first place.

Understanding print_r()

PHP provides us with a handy (if not a bit "dirty") function called print_r. What it does is print readable information about PHP objects and arrays that assist with debugging and figuring out what value we're trying to target. Here is a partial output of me doing a print_r($form) in a hook_form_alter of a blog post. What we need to understand is I am printing the full output of $form, which is an array. Each piece of the array contains a key and a value, where the value can be another array, an object, or a string.

Take a look at the screenshot above. When I am manipulating this form, I need to target the keys of the array(s) in order to manipulate any of the values. Each indent shows us the tree. We know that this is printing out $form. In order to target any of the keys within that, we need to use $form['nid'] or $form['type'] or $form['uid'], and deeper than that is $form['nid']['#type'], $form['type']['#value'], or $form['uid']['#value'].

Now we understand that arrays are handled with brackets: [ ]

But PHP objects are a little different. Below is a screenshot of another part of the $form array I posted above. This portion is accessed with $form['#node']. You'll notice instead of Array, it says stdClass Object. This means we're working with an object, not an array. Let's ignore what the differences are right now, and just figure out how we manipulate that data.

Arrays are targetted with brackets. Objects are targetted with ->.

Homework: A few other useful PHP functions are var_dump() and var_export().

Using dpm()

The Devel module takes this a bit further and provides us with dpm(). This gives us output in a much more readable format, though the concept is the same.

Hướng dẫn làm việc Arrays và Objects trong Drupal with print_r() and dpm()

With this, you can now use many of Drupal's great hooks to override various content-- take a look at my previous blog posts, Drupal Alters and Overrides: hook_form_alter and Drupal Alters and Overrides: hook_menu_alter.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Giới thiệu những sản phẩm di động tốt nhất tại CES 2014

Giới thiệu những sản phẩm di động tốt nhất tại CES 2014

Phiên bản thu nhỏ của Xperia Z1, máy tính chạy hai hệ điều hành của Asus, máy tính bảng lớn nhất của Samsung, sạc máy tính siêu nhỏ là những sản phẩm được đánh giá cao tại triển lãm Hàng điện tử tiêu dùng diễn ra tại Las Vegas (Mỹ) vừa qua.

Huawei Vision Running Android 2.3 Gingerbread

Huawei Vision Running Android 2.3 Gingerbread

Huawei has been pushing through with their low-cost android devices. Now, they are coming with Huawei Vision.

Bí quyết bán hành mỹ phẩm trên facebook cá nhân chất nhất 2019

Bí quyết bán hàng mỹ phẩm trên facebook cá nhân chất nhất 2019

Mua Follow; Từ khóa hành vi tìm kiếm ở phần mô tả; Từ khóa tìm kiếm ở tên phụ

Công ty diệt chuột T&C

 

Diet con trung