You are here

class FeedsTermProcessor in Feeds 8.2

Defines a taxonomy term processor.

Creates Taxonomy terms from feed items.

Plugin annotation


@Plugin(
  id = "taxonomy_term",
  title = @Translation("Taxonomy term processor"),
  description = @Translation("Creates taxonomy terms from feed items.")
)

Hierarchy

Expanded class hierarchy of FeedsTermProcessor

2 string references to 'FeedsTermProcessor'
feeds_update_7208 in ./feeds.install
Update to use generic bundle handling.
_feeds_feeds_plugins in ./feeds.plugins.inc
Break out for feeds_feed_plugins().

File

lib/Drupal/feeds/Plugin/feeds/processor/FeedsTermProcessor.php, line 30
FeedsTermProcessor class.

Namespace

Drupal\feeds\Plugin\feeds\processor
View source
class FeedsTermProcessor extends FeedsProcessor {

  /**
   * Define entity type.
   */
  public function entityType() {
    return 'taxonomy_term';
  }

  /**
   * Implements parent::entityInfo().
   */
  protected function entityInfo() {
    $info = parent::entityInfo();
    $info['label plural'] = t('Terms');
    $info['bundle name'] = t('Vocabulary');
    return $info;
  }

  /**
   * Creates a new term in memory and returns it.
   *
   * @todo Use entityNG.
   */
  protected function newEntity(FeedsSource $source) {
    return entity_create('taxonomy_term', array(
      'vid' => $this
        ->bundle(),
      'format' => isset($this->config['input_format']) ? $this->config['input_format'] : filter_fallback_format(),
    ))
      ->getBCEntity();
  }

  /**
   * Validates a term.
   */
  protected function entityValidate($term) {
    if (drupal_strlen($term
      ->label()) == 0) {
      throw new FeedsValidationException(t('Term name missing.'));
    }
  }

  /**
   * Saves a term.
   *
   * We de-array parent fields with only one item.
   * This stops leftandright module from freaking out.
   */
  protected function entitySave($term) {
    if (isset($term->parent)) {
      if (is_array($term->parent) && count($term->parent) == 1) {
        $term->parent = reset($term->parent);
      }
      if ($term
        ->id() && ($term->parent == $term
        ->id() || is_array($term->parent) && in_array($term
        ->id(), $term->parent))) {
        throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array(
          '@guid' => $term->feeds_item->guid,
        )));
      }
    }
    $term
      ->save();
  }

  /**
   * Deletes a series of terms.
   */
  protected function entityDeleteMultiple($tids) {
    entity_delete_multiple($this
      ->entityType(), $tids);
  }

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'vocabulary' => 0,
    ) + parent::configDefaults();
  }

  /**
   * Override setTargetElement to operate on a target item that is a taxonomy term.
   */
  public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value) {
    switch ($target_element) {
      case 'parent':
        if (!empty($value)) {
          $terms = taxonomy_term_load_multiple_by_name($value);
          $parent_tid = '';
          foreach ($terms as $term) {
            if ($term->vid == $target_term->vid) {
              $parent_tid = $term->tid;
            }
          }
          if (!empty($parent_tid)) {
            $target_term->parent[] = $parent_tid;
          }
          else {
            $target_term->parent[] = 0;
          }
        }
        else {
          $target_term->parent[] = 0;
        }
        break;
      case 'parentguid':

        // value is parent_guid field value
        $query = db_select('feeds_item')
          ->fields('feeds_item', array(
          'entity_id',
        ))
          ->condition('entity_type', $this
          ->entityType());
        $parent_tid = $query
          ->condition('guid', $value)
          ->execute()
          ->fetchField();
        $target_term->parent[] = $parent_tid ? $parent_tid : 0;
        break;
      case 'weight':
        if (!empty($value)) {
          $weight = intval($value);
        }
        else {
          $weight = 0;
        }
        $target_term->weight = $weight;
        break;
      default:
        parent::setTargetElement($source, $target_term, $target_element, $value);
        break;
    }
  }

  /**
   * Return available mapping targets.
   */
  public function getMappingTargets() {
    $targets = parent::getMappingTargets();
    $targets += array(
      'name' => array(
        'name' => t('Term name'),
        'description' => t('Name of the taxonomy term.'),
        'optional_unique' => TRUE,
      ),
      'parent' => array(
        'name' => t('Parent: Term name'),
        'description' => t('The name of the parent taxonomy term.'),
        'optional_unique' => TRUE,
      ),
      'parentguid' => array(
        'name' => t('Parent: GUID'),
        'description' => t('The GUID of the parent taxonomy term.'),
        'optional_unique' => TRUE,
      ),
      'weight' => array(
        'name' => t('Term weight'),
        'description' => t('Weight of the taxonomy term.'),
        'optional_unique' => TRUE,
      ),
      'description' => array(
        'name' => t('Term description'),
        'description' => t('Description of the taxonomy term.'),
      ),
    );

    // Let implementers of hook_feeds_term_processor_targets() add their targets.
    try {
      self::loadMappers();
      $entity_type = $this
        ->entityType();
      $bundle = $this
        ->bundle();
      drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
    } catch (Exception $e) {

      // Do nothing.
    }
    return $targets;
  }

  /**
   * Get id of an existing feed item term if available.
   */
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
    if ($tid = parent::existingEntityId($source, $result)) {
      return $tid;
    }

    // The only possible unique target is name.
    foreach ($this
      ->uniqueTargets($source, $result) as $target => $value) {
      if ($target == 'name') {
        if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(
          ':name' => $value,
          ':vid' => $this
            ->bundle(),
        ))
          ->fetchField()) {
          return $tid;
        }
      }
    }
    return 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property
FeedsConfigurable::$id protected property
FeedsConfigurable::$instances public static property
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration.
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 2
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::existing public function Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. 1
FeedsConfigurable::getConfig public function Implements getConfig(). 1
FeedsConfigurable::instance public static function Instantiate a FeedsConfigurable object. 1
FeedsConfigurable::setConfig public function Set configuration.
FeedsConfigurable::__get public function Override magic method __get(). Make sure that $this->config goes through getConfig().
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsPlugin::all public static function Get all available plugins.
FeedsPlugin::byType public static function Gets all available plugins of a particular type.
FeedsPlugin::child public static function Determines whether given plugin is derived from given base plugin.
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::loadMappers public static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save
FeedsPlugin::sourceDefaults public function Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::sourceDelete public function A source is being deleted. Overrides FeedsSourceInterface::sourceDelete 2
FeedsPlugin::sourceForm public function Callback methods, exposes source form. Overrides FeedsSourceInterface::sourceForm 3
FeedsPlugin::sourceFormValidate public function Validation handler for sourceForm. Overrides FeedsSourceInterface::sourceFormValidate 2
FeedsPlugin::sourceSave public function A source is being saved. Overrides FeedsSourceInterface::sourceSave 2
FeedsPlugin::typeOf public static function Determines the type of a plugin.
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct
FeedsProcessor::bundle public function Bundle type this processor operates on.
FeedsProcessor::bundleOptions public function Provides a list of bundle options for use in select lists.
FeedsProcessor::clear public function Remove all stored results or stored results up to a certain time for a source.
FeedsProcessor::configForm public function Overrides parent::configForm(). Overrides FeedsConfigurable::configForm 2
FeedsProcessor::createLogMessage protected function Creates a log message for when an exception occured during import.
FeedsProcessor::entityLoad protected function Load an existing entity. 2
FeedsProcessor::entitySaveAccess protected function Access check for saving an enity. 1
FeedsProcessor::expire public function Deletes feed items older than REQUEST_TIME - $time.
FeedsProcessor::expiryQuery protected function Returns a database query used to select entities to expire. 1
FeedsProcessor::expiryTime public function Per default, don't support expiry. If processor supports expiry of imported items, return the time after which items should be removed. 1
FeedsProcessor::getHash protected function Retrieves the MD5 hash of $entity_id from the database.
FeedsProcessor::getLimit public function
FeedsProcessor::getMappings public function Get mappings.
FeedsProcessor::hash protected function Create MD5 hash of item and mappings array.
FeedsProcessor::itemCount public function Counts the number of items imported by this processor.
FeedsProcessor::loadItemInfo protected function Loads existing entity information and places it on $entity->feeds_item.
FeedsProcessor::map protected function Execute mapping on an item.
FeedsProcessor::newItemInfo protected function Adds Feeds specific information on $entity->feeds_item.
FeedsProcessor::pluginType public function Implements FeedsPlugin::pluginType(). Overrides FeedsPlugin::pluginType
FeedsProcessor::process public function Process the result of the parsing stage.
FeedsProcessor::uniqueTargets protected function Utility function that iterates over a target array and retrieves all sources that are unique.
FeedsTermProcessor::configDefaults public function Override parent::configDefaults(). Overrides FeedsProcessor::configDefaults
FeedsTermProcessor::entityDeleteMultiple protected function Deletes a series of terms. Overrides FeedsProcessor::entityDeleteMultiple
FeedsTermProcessor::entityInfo protected function Implements parent::entityInfo(). Overrides FeedsProcessor::entityInfo
FeedsTermProcessor::entitySave protected function Saves a term. Overrides FeedsProcessor::entitySave
FeedsTermProcessor::entityType public function Define entity type. Overrides FeedsProcessor::entityType
FeedsTermProcessor::entityValidate protected function Validates a term. Overrides FeedsProcessor::entityValidate
FeedsTermProcessor::existingEntityId protected function Get id of an existing feed item term if available. Overrides FeedsProcessor::existingEntityId
FeedsTermProcessor::getMappingTargets public function Return available mapping targets. Overrides FeedsProcessor::getMappingTargets
FeedsTermProcessor::newEntity protected function Creates a new term in memory and returns it. Overrides FeedsProcessor::newEntity
FeedsTermProcessor::setTargetElement public function Override setTargetElement to operate on a target item that is a taxonomy term. Overrides FeedsProcessor::setTargetElement