Practice assignment - writing a custom module for Drupal 7 that parses header information from Worpress sites. Custom code showcases my knowledge of how to use entity metadata wrappers and several hooks: hook_menu(), hook_form_alter(), and hook_node_view().
Completion Date
Code Snippet
diff --git a/docroot/sites/all/modules/custom/sample1/sample1.info b/docroot/sites/all/modules/custom/sample1/sample1.info new file mode 100644 index 0000000..4b7e84a --- /dev/null +++ b/docroot/sites/all/modules/custom/sample1/sample1.info @@ -0,0 +1,6 @@ +name = Sample Wordpress Validator - 2nd pass +description = A module that validates Wordpress header data. +core = 7.x +package = Sample +version = 0.1-dev +dependencies[] = entityreference diff --git a/docroot/sites/all/modules/custom/sample1/sample1.module b/docroot/sites/all/modules/custom/sample1/sample1.module new file mode 100644 index 0000000..c78a309 --- /dev/null +++ b/docroot/sites/all/modules/custom/sample1/sample1.module @@ -0,0 +1,129 @@ +<?php +/** + * Implementation of hook_menu() + */ +function sample1_menu() { + $items = array(); + $items['node/%node/validate'] = array( + 'title' => 'Validate', + 'description' => 'Validate this site', + 'page callback' => 'sample1_validate_wp', + 'page arguments' => array(1), + 'access callback' => 'sample1_access_callback', + 'access arguments' => array(1), + 'type' => MENU_LOCAL_TASK, + 'weight' => 10, + ); + $items['node/%node/history'] = array( + 'title' => 'History', + 'description' => 'History Report of past validation checks', + 'page callback' => 'sample1_history', + 'access callback' => 'sample1_access_callback', + 'access arguments' => array(1), + 'type' => MENU_LOCAL_TASK, + 'weight' => 11, + ); + return $items; +} + +/** + * Check the corresponding WP site and store results in a new 'Results' node + */ +function sample1_validate_wp($ref_node) { + + global $user; + + $values = array( + 'type' => 'result', + 'uid' => $user->uid, + 'status' => 1, + 'comment' => 0, + 'promote' => 0, + ); + + // Declare the new node + $entity = entity_create('node', $values); + + // Wrap it + $ewrapper = entity_metadata_wrapper('node', $entity); + + //Set the title + $ewrapper->title->set($ref_node->title . " - " . date("m/d/Y H:i",time())); + + // Set the 'field_site' value + $ewrapper->field_site->set($ref_node); + + // Wrap the reference node + $ref_wrapper = entity_metadata_wrapper('node',$ref_node); + + // Run the WP version check + $wp_meta_array = get_meta_tags($ref_wrapper->field_url->url->value()); + $wp_result = empty($wp_meta_array['generator']) ? 'Version not found' : $wp_meta_array['generator']; + $ewrapper->field_result->set($wp_result); + + // Save the new node + $ewrapper->save(); + + // Point the Latest Result field to the new result node + $ref_wrapper->field_latest_result->set($ewrapper->getIdentifier()); + + // Set the 'active' flag to Yes or No on the reference node + $ref_wrapper->field_active->set(empty($wp_meta_array['generator']) ? false : true); + + // Save the updated reference node + $ref_wrapper->save(); + + // Goto the new node and display a status message + drupal_set_message(t("Validation complete.")); + return drupal_goto("/node/" . $ewrapper->getIdentifier()); +} + +/** + * Implementation of hook_form_alter() + */ +function sample1_form_alter(&$form, &$form_state, $form_id) { + switch($form_id){ + // Hide certain fields when editing Site nodes + case 'wordpress_site_node_form': + unset($form['field_active']); + unset($form['field_latest_result']); + } +} + +function sample1_access_callback($node) { + return $node->type == 'wordpress_site' && user_access('access content'); +} + +/** + * Implementation of hook_node_view() + */ +function sample1_node_view($node, $view_mode, $langcode) { + if ($node->type == 'wordpress_site' && $view_mode == 'teaser') { + $rslt_nid = isset($node->field_latest_result[$node->language][0]['target_id']) ? $node->field_latest_result[$node->language][0]['target_id'] : null; + + // Display extra information if a result reference nid is listed + if ($rslt_nid != null) { + // Set up a wrapper + $rslt_wrapper = entity_metadata_wrapper('node',$rslt_nid); + + // Display the 'wordpress version' value based on the result node's creation date + $node->content['result'] = array( + '#markup' => "<strong>Last Result:</strong> " . $rslt_wrapper->field_result->value() . "<br />", + '#weight' => 11, + ); + + // Display the 'last updated' value based on the result node's creation date + $node->content['last_updated'] = array( + '#markup' => "<strong>Last Checked:</strong> " . format_date($rslt_wrapper->created->value(), 'long') . "<br />", + '#weight' => 12, + ); + } + } +} + +/** + * Render a blank page which will be used to expose the history views block + */ +function sample1_history() { + return ""; +}