You are here

function yoast_seo_configuration_save in Real-time SEO for Drupal 7

Save an entity's SEO settings.

Parameters

string $entity_type: The entity type to load.

int $entity_id: The entity's ID.

int $revision_id: The entity's VID.

array $seo_info: All of the SEO information.

string $langcode: The language of the translation set.

int $old_vid: (optional) The old revision.

2 calls to yoast_seo_configuration_save()
yoast_seo_entity_insert in ./yoast_seo.module
Implements hook_entity_insert().
yoast_seo_entity_update in ./yoast_seo.module
Implements hook_entity_update().

File

./yoast_seo.module, line 794
Primary hook implementations for Yoast SEO for Drupal module.

Code

function yoast_seo_configuration_save($entity_type, $entity_id, $revision_id, $seo_info, $langcode, $old_vid = NULL) {

  // If no language assigned, or the language doesn't exist, use the
  // has-no-language language.
  $languages = language_list();
  if (empty($langcode) || !isset($languages[$langcode])) {
    $langcode = LANGUAGE_NONE;
  }

  // Check that $entity_id is numeric because of Entity API and string IDs.
  if (!is_numeric($entity_id)) {
    return;
  }

  // The revision_id must be a numeric value; some entities use NULL for the
  // revision so change that to a zero.
  if (is_null($revision_id)) {
    $revision_id = 0;
  }

  // Handle scenarios where the seo info is completely empty.
  if (empty($seo_info)) {
    $seo_info = array();

    // Add an empty array record for each language.
    $languages = db_query("SELECT language, ''\n        FROM {yoast_seo}\n        WHERE (entity_type = :type)\n        AND (entity_id = :id)\n        AND (revision_id = :revision)", array(
      ':type' => $entity_type,
      ':id' => $entity_id,
      ':revision' => $revision_id,
    ))
      ->fetchAllKeyed();
    foreach ($languages as $oldlang => $empty) {
      $seo_info[$oldlang] = array();
    }
  }

  // Update each of the per-language SEO info configurations in turn.
  foreach ($seo_info as $langcode => $new_seo_info) {

    // If the data array is empty, there is no data to actually save, so just
    // delete the record from the database.
    if (empty($new_seo_info)) {
      db_delete('yoast_seo')
        ->condition('entity_type', $entity_type)
        ->condition('entity_id', $entity_id)
        ->condition('revision_id', $revision_id)
        ->condition('language', $langcode)
        ->execute();
    }
    else {
      db_merge('yoast_seo')
        ->key(array(
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'language' => $langcode,
        'revision_id' => $revision_id,
      ))
        ->fields(array(
        'focus_keyword' => $new_seo_info['focus_keyword'],
        'seo_status' => $new_seo_info['seo_status'],
      ))
        ->execute();
    }
  }
}