Skip to content
Snippets Groups Projects
FeedsProcessor.inc 47 KiB
Newer Older
Chris Leppanen's avatar
Chris Leppanen committed
/**
 * @file
 * Contains FeedsProcessor and related classes.
 */
// Insert mode for new items.
define('FEEDS_SKIP_NEW', 0);
define('FEEDS_INSERT_NEW', 1);

// Update mode for existing items.
define('FEEDS_SKIP_EXISTING', 0);
define('FEEDS_REPLACE_EXISTING', 1);
define('FEEDS_UPDATE_EXISTING', 2);
// Options for handling content in Drupal but not in source data.
define('FEEDS_SKIP_NON_EXISTENT', 'skip');
define('FEEDS_DELETE_NON_EXISTENT', 'delete');
// Default limit for creating items on a page load, not respected by all
// processors.
define('FEEDS_PROCESS_LIMIT', 50);

/**
 * Thrown if a validation fails.
 */
class FeedsValidationException extends Exception {}

/**
 * Thrown if a an access check fails.
 */
class FeedsAccessException extends Exception {}

/**
 * Abstract class, defines interface for processors.
 */
abstract class FeedsProcessor extends FeedsPlugin {
  public function pluginType() {
  /**
   * @defgroup entity_api_wrapper Entity API wrapper.
   */

  /**
   * Entity type this processor operates on.
   */
  public abstract function entityType();

  /**
   * Bundle type this processor operates on.
   *
   * Defaults to the entity type for entities that do not define bundles.
   *
   *   The bundle type this processor operates on, or NULL if it is undefined.
   */
  public function bundle() {
    return $this->config['bundle'];
  }

  /**
   * Provides a list of bundle options for use in select lists.
   *
   * @return array
   *   A keyed array of bundle => label.
   */
  public function bundleOptions() {
    $options = array();
    foreach (field_info_bundles($this->entityType()) as $bundle => $info) {
      if (!empty($info['label'])) {
        $options[$bundle] = $info['label'];
      }
      else {
        $options[$bundle] = $bundle;
      }
    }
    return $options;
  }

  /**
   * Provides a list of languages available on the site.
   *
   * @return array
   *   A keyed array of language_key => language_name (example: 'en' => 'English').
   */
  public function languageOptions() {
    $languages = array(
      LANGUAGE_NONE => t('Language neutral'),
    );
    $language_list = language_list('enabled');
    if (!empty($language_list[1])) {
      foreach ($language_list[1] as $language) {
        $languages[$language->language] = $language->name;
      }
    }
    return $languages;
  }

   *   The feeds source that spawns this entity.
   *
  protected function newEntity(FeedsSource $source) {
    $entity = new stdClass();

    $info = $this->entityInfo();
    if (!empty($info['entity keys']['language'])) {
      $entity->{$info['entity keys']['language']} = $this->entityLanguage();
    }

    return $entity;
  }

  /**
   * Load an existing entity.
   *
   * @param $source
   *   The feeds source that spawns this entity.
   * @param $entity_id
   *   The unique id of the entity that should be loaded.
   *
   * @return
   *   A new entity object.
   *
   * @todo We should be able to batch load these, if we found all of the
   *   existing ids first.
  protected function entityLoad(FeedsSource $source, $entity_id) {
    if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
      $entities = entity_load($this->entityType(), array($entity_id));
      $entity = reset($entities);
    }
    else {
      $args = array(':entity_id' => $entity_id);
      $table = db_escape_table($info['base table']);
      $key = db_escape_field($info['entity keys']['id']);
      $entity = db_query("SELECT * FROM {" . $table . "} WHERE $key = :entity_id", $args)->fetchObject();
    if ($entity && !empty($info['entity keys']['language'])) {
      $entity->{$info['entity keys']['language']} = $this->entityLanguage();
    }
   * @throws FeedsValidationException $e
  protected function entityValidate($entity) {
    $info = $this->entityInfo();

    if (!empty($info['entity keys']['language'])) {
      // Ensure that a valid language is always set.
      $key = $info['entity keys']['language'];
      if (empty($entity->$key) || !isset($languages[$entity->$key])) {
        $entity->$key = $this->entityLanguage();
    }

    // Perform field validation if entity is fieldable.
    if (!empty($info['fieldable'])) {
      try {
      }
      catch (FieldValidationException $e) {
        $errors = array();

        // Unravel the errors inside the FieldValidationException.
        foreach ($e->errors as $field_name => $field_errors) {
          foreach ($field_errors as $langcode => $field_item_errors) {
            $errors = array_merge($errors, $this->unravelFieldValidationExceptionErrors($field_item_errors));
          }
        }

        // Compose error message. If available, use the entity label to indicate
        // which item failed. Fallback to the GUID value (if available) or else
        // no indication.
        $label = entity_label($this->entityType(), $entity);
Loading
Loading full blame...