Hi Guys,

I have created a custom module with the intention to check if a certain field value has a "broken link". I'm posting vacancies on my website with a link to an external website. In the case the external webpage is deleted or not existing i want to run a cron job to de-publish the content. I want to know how to retrieve the field value.

I have a content type "Vacancy" and a field name "vacancy_link". How can i get the value of this field in my .module file?

I have tried losts of thing but nothing worked.

My function looks like this. I'm using EntityFieldQuery to lookup the nodes a loop through all of the nodes.

Any help i highly appreciated!

function node_depublisher_handler() {

// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'vacancy')
  ->propertyCondition('status', 1);

  $result = $query->execute();
  $nids = array_keys($result['node']);

  
  // Load all nodes in one go for better performance.
  $nodes = node_load_multiple($nids);
  
  foreach ($nodes as $node) {

    if (in case of a 404 ) {
    $node->status = 0;
    // re-save the node
    node_save($node);
    }
  }
}

Comments

nevets’s picture

You might want to look at Link Checker

As for you question, I would recommend using the Entity API module and entity_metadata_wrapper().

Using it, once you have a node you can do

$wrapper = entity_metadata_wrapper('node', $node);
$link = $wrapper->field_vacancy_link->value();
// Note depending on the field type, $link may be a structure
Jaypan’s picture

You can do this:

foreach ($nodes as $node) {
  $link = $node->field_vacancy_link[LANGUAGE_NONE][0]['value'];
  $request = drupal_http_request($link);
  if($request->code == 404)
  {
    $node->status = 0;
    // re-save the node
    node_save($node);
  }
}

Note that ['value'] may be incorrect. Dump the value of $node->field_vacancy_link[LANGUAGE_NONE][0] to see what it actually contains.

You can also use the entity_metadata_wrapper as nevets suggested. It's quite handy. The concept ends up being the same.