You are here

flag.install in Flag 8.4

Flag module install/schema/update hooks.

File

flag.install
View source
<?php

/**
 * @file
 * Flag module install/schema/update hooks.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;

/**
 * Implements hook_schema().
 */
function flag_schema() {
  $schema = [];
  $schema['flag_counts'] = [
    'description' => 'The number of times an item has been flagged.',
    'fields' => [
      'flag_id' => [
        'type' => 'varchar_ascii',
        'length' => '32',
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'The flag type, for example "node", "comment", or "user".',
        'type' => 'varchar_ascii',
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'The unique ID of the flagged entity, for example the uid, cid, or nid.',
        'type' => 'varchar_ascii',
        'length' => ConfigEntityStorage::MAX_ID_LENGTH,
        'not null' => TRUE,
      ],
      'count' => [
        'description' => 'The number of times this object has been flagged for this flag.',
        'type' => 'int',
        'unsigned' => TRUE,
      ],
      'last_updated' => [
        'description' => 'The UNIX time stamp representing when the flag was last updated.',
        'type' => 'int',
        'unsigned' => TRUE,
        'disp-size' => 11,
      ],
    ],
    'primary key' => [
      'flag_id',
      'entity_id',
    ],
    'indexes' => [
      'flag_id_entity_type' => [
        'flag_id',
        'entity_type',
      ],
      'entity_type_entity_id' => [
        'entity_type',
        'entity_id',
      ],
      'flag_id_count' => [
        'flag_id',
        'count',
      ],
      'flag_id_last_updated' => [
        'flag_id',
        'last_updated',
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function flag_requirements($phase) {
  $requirements = [];

  /*
  if ($phase == 'runtime') {
    if (\Drupal::moduleHandler()->moduleExists('translation') && !\Drupal::moduleHandler()->moduleExists('translation_helpers')) {
      $requirements['flag_translation'] = array(
        'title' => t('Flag'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('To have the flag module work with translations, you need to install and enable the <a href="http://drupal.org/project/translation_helpers">Translation helpers</a> module.'),
        'value' => t('Translation helpers module not found.'),
      );
    }
    if (\Drupal::moduleHandler()->moduleExists('session_api')) {
      if (file_exists('./robots.txt')) {
        $flag_path = url('flag') . '/';
        // We don't use url() because this may return an absolute URL when
        // language negotiation is set to 'domain'.
        $flag_path = parse_url($flag_path, PHP_URL_PATH);
        $robots_string = 'Disallow: ' . $flag_path;
        $contents = file_get_contents('./robots.txt');
        if (strpos($contents, $robots_string) === FALSE) {
          $requirements['flag_robots'] = array(
            'title' => t('Flag robots.txt problem'),
            'severity' => REQUIREMENT_WARNING,
            'description' => t('Flag module may currently be used with anonymous users, however the robots.txt file does not exclude the "@flag-path" path, which may cause search engines to randomly flag and unflag content when they index the site. It is highly recommended to add "@robots-string" to your robots.txt file (located in the root of your Drupal installation).', array('@flag-path' => $flag_path, '@robots-string' => $robots_string)),
            'value' => t('Search engines flagging content'),
          );
        }
      }
    }
  }
  */
  return $requirements;
}

/**
 * Fix schema changes in 4.0-alpha2.
 */
function flag_update_8401() {
  $bundle_schema = [
    'description' => 'The Flag ID.',
    'type' => 'varchar_ascii',
    'length' => \Drupal\Core\Entity\EntityTypeInterface::BUNDLE_MAX_LENGTH,
    // 'binary' => FALSE,
    'not null' => TRUE,
  ];

  /** @var \Drupal\Core\Database\Schema $schema */
  $schema = \Drupal::database()
    ->schema();
  $schema
    ->changeField('flagging', 'flag_id', 'flag_id', $bundle_schema);
  $schema
    ->dropIndex('flagging', 'flag_id');
  $schema
    ->dropIndex('flagging', 'flagging_field__flag_id__target_id');
  $schema
    ->addIndex('flagging', 'flagging_field__flag_id__target_id', [
    'flag_id',
  ], [
    'fields' => [
      'flag_id' => $bundle_schema,
    ],
  ]);

  // Update the field storage repository.

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $efm */
  $efm = \Drupal::service('entity_field.manager');

  /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $kv */
  $kv = \Drupal::service('keyvalue');

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $repo */
  $repo = \Drupal::service('entity.last_installed_schema.repository');
  $efm
    ->clearCachedFieldDefinitions();
  $storage_definition = $efm
    ->getFieldStorageDefinitions('flagging')['flag_id'];
  $repo
    ->setLastInstalledFieldStorageDefinition($storage_definition);

  // Update the stored field schema.
  // @todo: There has to be a better way to do this.
  $kv_collection = 'entity.storage_schema.sql';
  $kv_name = 'flagging.field_schema_data.flag_id';
  $field_schema = $kv
    ->get($kv_collection)
    ->get($kv_name);
  $field_schema['flagging']['fields']['flag_id'] = $bundle_schema;
  $field_schema['flagging']['indexes']['flagging_field__flag_id__target_id'] = [
    'flag_id',
  ];
  $kv
    ->get($kv_collection)
    ->set($kv_name, $field_schema);
}

Functions

Namesort descending Description
flag_requirements Implements hook_requirements().
flag_schema Implements hook_schema().
flag_update_8401 Fix schema changes in 4.0-alpha2.