You are here

realistic_dummy_content_api.module in Realistic Dummy Content 8

API code allowing other modules to generate realistic dummy content. See the Realistic Dummy Content module for an example of how to use.

File

api/realistic_dummy_content_api.module
View source
<?php

/**
 * @file
 *
 * API code allowing other modules to generate realistic dummy content.
 * See the Realistic Dummy Content module for an example of how to use.
 */
use Drupal\realistic_dummy_content_api\facade\RealisticDummyContent;

/**
 * Implements hook_entity_insert().
 */
function realistic_dummy_content_api_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
  $return = RealisticDummyContent::entity_presave($entity);
  return $return;
}

/**
 * Implements hook_realistic_dummy_content_attribute_manipulator_alter().
 */
function realistic_dummy_content_api_realistic_dummy_content_attribute_manipulator_alter(&$class, &$type, &$machine_name) {
  return RealisticDummyContent::realistic_dummy_content_attribute_manipulator_alter($class, $type, $machine_name);
}

/**
 * Checks if a given entity is dummy content.
 *
 * @param $entity
 *   The object for a given entity type, for example this can be a user object
 *   or a node object.
 * @param $type
 *   The type of the information to change, for example 'user' or 'node'.
 *
 * @return
 *   TRUE if at least one module implemented hook_realistic_dummy_content_api_dummy
 *   and thinks the entity is a dummy objects; FALSE otherwise.
 */
function realistic_dummy_content_api_is_dummy($entity, $type) {
  return RealisticDummyContent::is_dummy($entity, $type);
}

/**
 * Insert or improve dummy data in an entity of a given type.
 *
 * @param $entity
 *   The object for a given type, for example this can be a user object
 *   or a node object.
 * @param $type
 *   The type of the information to change, for example 'user' or 'node'.
 *
 * @throws
 *   \Exception
 */
function realistic_dummy_content_api_improve_dummy_content(&$entity, $type) {
  return RealisticDummyContent::improve_dummy_content($entity, $type);
}

/**
 * Throw an \Exception if an entity is not valid
 */
function realistic_dummy_content_api_validate($entity, $type) {
  return RealisticDummyContent::validate($entity, $type);
}

/**
 * Validate that a class is a valid subclasss of Base
 *
 * @param $class
 *   A class name
 *
 * @throws
 *   \Exception
 */
function realistic_dummy_content_api_validate_class($class) {
  return RealisticDummyContent::validate_class($class);
}

/**
 * Implements hook_realistic_dummy_content_api_class().
 */
function realistic_dummy_content_api_realistic_dummy_content_api_class($entity, $type) {
  return RealisticDummyContent::realistic_dummy_content_api_class($entity, $type);
}

/**
 * Implements hook_realistic_dummy_content_api_dummy().
 */
function realistic_dummy_content_api_realistic_dummy_content_api_dummy($entity, $type) {
  return RealisticDummyContent::realistic_dummy_content_api_dummy($entity, $type);
}

/**
 * Generate a random, or sequential, number
 *
 * By default, this function will return a random number between $start and $end
 * inclusively. If you set the realistic_dummy_content_api_rand variable to
 * REALISTIC_DUMMY_CONTENT_SEQUENTIAL, for example for automated tested or in a recipe
 * (an example can be found at realistic_dummy_content/recipe/realistic_dummy_content.recipe.inc),
 * then this will call realistic_dummy_content_api_sequential().
 *
 * See the documentation for realistic_dummy_content_api_sequential() for details.
 *
 * @param $start
 *   The first possible number in the range.
 * @param $end
 *   The last possible number in the range.
 * @param $hash = NULL
 *   Ignored for random numbers; for sequential numbers, please se the documentation for
 *   realistic_dummy_content_api_sequential() for details.
 *
 * @return
 *   A random number by default, or a sequential number if you set the
 *   realistic_dummy_content_api_rand variable to REALISTIC_DUMMY_CONTENT_SEQUENTIAL.
 *   Please see the description of realistic_dummy_content_api_sequential() for details.
 */
function realistic_dummy_content_api_rand($start, $end, $hash = NULL) {
  return RealisticDummyContent::rand($start, $end, $hash);
}

/**
 * Generate sequential number based on a hash
 *
 * Returns the starting number on every call until the hash is changed, at which case it
 * returns the second number, and so on.
 *
 * The idea behind this is that for a single node, we might want to retrieve the
 * 3rd file for each field (they go together).
 *
 * In the above example, if the 3rd file does not exist, we will return the first file,
 * in order to never return a number which is outside the range of start to end.
 *
 * @param $start
 *   The first possible number in the range.
 * @param $end
 *   The last possible number in the range.
 * @param $hash
 *   The number returned by this function will be in sequence: each call to
 *   realistic_dummy_content_api_sequential()'s return is incremented by
 *   one, unless $hash is the same as in the last call, in which case the return will the
 *   same as in the last call.
 *
 * @return
 *   A sequential number based on the $hash.
 *   Please see the description of the $hash parameter, above.
 */
function realistic_dummy_content_api_sequential($start, $end, $hash) {
  return RealisticDummyContent::sequential($start, $end, $hash);
}

/**
 * Attempts to generate all realistic content for the current site.
 *
 * @param $log
 *   A class which implements the interface Log. Logging
 *   will be different if you are using drush or in the context of an automated
 *   test, for example.
 */
function realistic_dummy_content_api_apply_recipe($log) {
  return RealisticDummyContent::apply_recipe($log);
}