1

I have a content type called Country and I have to verify what countries already exists in db and save the missing ones, comparing to a variable with all countries ($country_list). I am having trouble accessing countries names of my nodes, that are specified as the content type title.

  $country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];
  $nodes = node_load_multiple(array(), array('type' => 'country_profile'));
  $nodes_name = node_title_list($nodes);

  foreach ($country_list as $country_name) {
    if (!in_array($nodes_name, $nodes)) {
      // should create new node
    } else {
      // shouldn't do anything
    }
  }

The problem is, when i get the node_title_list() it doesn't return the 'raw title' (see output below)

Output:

    Array
(
    [#theme] => item_list__node
    [#items] => Array
        (
            [0] => <a href="/discover/country_profiles/tanzania">Tanzania</a>
            [1] => <a href="/discover/country_profiles/mexico">Mexico</a>
        )

    [#title] => 1
)

How can i do that, so that the output of this list is

Array
        (
            [0] => "Tanzania"
            [1] => "Mexico"
        )
1
0

If you just need to create the nodes that don't have a specific title (which I guess it's just a country name), you don't need to use node_title_list(), whose purpose is rendering a list of node titles as links. Just load the nodes and check which titles aren't already used.

$country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'country_profile')
  // Remove the following line if you don't need to get only published nodes.
  ->propertyCondition('status', NODE_PUBLISHED)
  // Keep the following line, so Drupal won't check if the currently logged-in user has access to the nodes and the fields.
  ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
$result = $query->execute();
if (isset($result['node'])) {
  $country_list = array_flip($country_list);
  $nodes = entity_load('node', array_keys($result['node']));
  foreach ($nodes as $node) {
    unset($country_list[$node->title]);
  }
  $country_list = array_flip($country_list);
}

if (!empty($country_list)) {
  // There are nodes that need to be created; their titles are in $country_list.
}

Even better, load those nodes whose title is equal to one of the country names you already have.

$country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'country_profile')
  // Remove the following line if you don't need to get only published nodes.
  ->propertyCondition('status', NODE_PUBLISHED)
  ->propertyCondition('title', $country_list)
  // Keep the following line, so Drupal won't check if the currently logged-in user has access to the nodes and the fields.
  ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
$result = $query->execute();
if (isset($result['node'])) {
  $country_list = array_flip($country_list);
  $nodes = entity_load('node', array_keys($result['node']));
  foreach ($nodes as $node) {
    unset($country_list[$node->title]);
  }
  $country_list = array_flip($country_list);
}

if (!empty($country_list)) {
  // There are nodes that need to be created; their titles are in $country_list.
}
4
  • Thanks, that makes sense. The only problem is, it only falls in the create nodes if the list is empty, what if just some of the countries were already created? – Fabíola Dec 14 '20 at 17:58
  • The code in the answer removes from the country list the nodes that already exist with that title. It's done from foreach ($nodes as $node) { unset($country_list[$node->title]); }. It also avoids to create nodes if the country list is empty: if (!empty($country_list)) { /* ... */ }. – kiamlaluno Dec 14 '20 at 18:00
  • (To be correct, the code should have used array_flip().) – kiamlaluno Dec 14 '20 at 18:44
  • Guess i wasn't paying enough attention, i am sorry. Tried using the code and worked! Tyvm. Used array_flip before if(isset...) – Fabíola Dec 14 '20 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.