Skip to content
entity_test.module 4.39 KiB
Newer Older
<?php

/**
 * @file
 * Test module for the entity API providing an entity type for testing.
 */

use Drupal\entity_test\Plugin\Core\Entity\EntityTest;


 * Implements hook_entity_info_alter().
function entity_test_entity_info_alter(&$info) {
  // Optionally specify a translation handler for testing translations.
  if (state()->get('entity_test.translation')) {
    $info['entity_test']['translation']['entity_test'] = TRUE;
/**
 * Implements hook_field_extra_fields().
 */
function entity_test_field_extra_fields() {
  $extra['entity_test']['entity_test'] = array(
    'display' => array(
      // Note: those extra fields do not currently display anything, they are
      // just used in \Drupal\entity\Tests\EntityDisplayTest to test the
      // behavior of entity display objects,
      'display_extra_field' => array(
        'label' => t('Display extra field'),
        'description' => t('An extra field on the display side.'),
        'weight' => 5,
        'visible' => TRUE,
      ),
      'display_extra_field_hidden' => array(
        'label' => t('Display extra field (hidden)'),
        'description' => t('An extra field on the display side, hidden by default.'),
        'visible' => FALSE,
      ),
    )
  );

  return $extra;
}

/**
 * Implements hook_permission().
 */
function entity_test_permission() {
  $permissions = array(
    'administer entity_test content' => array(
      'title' => t('Administer entity_test content'),
      'description' => t('Manage entity_test content'),
    ),
    'view test entity' => array(
      'title' => t('View test entities'),
    ),
    'view test entity translations' => array(
      'title' => t('View translations of test entities'),
    ),
  );
  return $permissions;
}

/**
 * Implements hook_menu().
 */
function entity_test_menu() {
  $items = array();

  $items['entity-test/add'] = array(
    'title' => 'Add an entity_test',
    'page callback' => 'entity_test_add',
    'access arguments' => array('administer entity_test content'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['entity-test/manage/%entity_test'] = array(
    'title' => 'Edit test entity',
    'page callback' => 'entity_test_edit',
    'page arguments' => array(2),
    'access arguments' => array('administer entity_test content'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['entity-test/manage/%entity_test/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  return $items;
}

/**
 * Menu callback: displays the 'Add new entity_test' form.
 *
 * @return array
 *   The processed form for a new entity_test.
 *
 * @see entity_test_menu()
 */
function entity_test_add() {
  drupal_set_title(t('Create an entity_test'));
  $entity = entity_create('entity_test', array());
  return entity_get_form($entity);
}

/**
 * Menu callback: displays the 'Edit existing entity_test' form.
 *
 * @param array $entity
 *   The entity to be edited.
 *
 * @return array
 *   The processed form for the edited entity_test.
 *
 * @see entity_test_menu()
function entity_test_edit(EntityTest $entity) {
  drupal_set_title($entity->label(), PASS_THROUGH);
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return Drupal\entity_test\Plugin\Core\Entity\EntityTest
 *   The loaded entity object, or FALSE if the entity cannot be loaded.
 */
function entity_test_load($id, $reset = FALSE) {
  return entity_load('entity_test', $id, $reset);
}

/**
 * Loads multiple test entities based on certain conditions.
 *
 * @param array $ids
 *   (optional) An array of entity IDs. If omitted, all entities are loaded.
 *   A boolean indicating that the internal cache should be reset.
 *
 *   An array of test entity objects, indexed by ID.
 */
function entity_test_load_multiple(array $ids = NULL, $reset = FALSE) {
  return entity_load_multiple('entity_test', $ids, $reset);
}

/**
 * Deletes multiple test entities.
 *
 * @param $ids
 *   An array of test entity IDs.
 */
function entity_test_delete_multiple(array $ids) {
  entity_delete_multiple('entity_test', $ids);

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) {
  $langcode = $form_state['controller']->getFormLangcode($form_state);
  variable_set('entity_form_langcode', $langcode);
}