You are here

photos.install in Album Photos 8.5

Install, update, and uninstall functions for the Photos module.

File

photos.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the Photos module.
 */
use Drupal\Core\Annotation\PluralTranslation;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_schema().
 */
function photos_schema() {

  // @todo migrate fid to {photos_image}.id cover_id.
  // @todo migrate pid to album_id.
  // @todo migrate wid to weight.
  $schema['photos_album'] = [
    'fields' => [
      'album_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'cover_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'weight' => [
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'count' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'data' => [
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
      ],
    ],
    'indexes' => [
      'cover_id' => [
        'cover_id',
      ],
      'weight' => [
        'weight',
      ],
    ],
    'primary key' => [
      'album_id',
    ],
  ];

  // @todo migrate photos_image to new photos_image entity.
  // @todo migrate comments to new photos_image entity comment field.
  // @todo migrate {photos_image}.count to {photos_count}. Note that
  // {photso_count} is counting photos and {photos_image}.count is counting
  // views.
  // @todo look into core statistics API to replace or supplement photos_count?
  // @see https://www.drupal.org/project/photos/issues/3101624
  $schema['photos_count'] = [
    'fields' => [
      'id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'cid' => [
        'type' => 'int',
        'description' => 'Count entity id.',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'changed' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'type' => [
        'type' => 'varchar',
        'length' => 12,
        'default' => '',
        'not null' => TRUE,
      ],
      'value' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'indexes' => [
      'cid' => [
        'cid',
      ],
      'type' => [
        'type',
      ],
      'value' => [
        'value',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function photos_install() {
  \Drupal::messenger()
    ->addMessage(t("Photos settings are available under admin/structure/photos."));
  \Drupal::messenger()
    ->addMessage(t("New permissions are available for Photos admin/people/permissions#module-photos."));
}

/**
 * Prep for new photos_image entity.
 */
function photos_update_8501(&$sandbox) {

  // Check if this update needs to run.
  if (\Drupal::database()
    ->schema()
    ->fieldExists('photos_image', 'fid')) {

    // Move photos_image to photos_image_tmp.
    // @see photos_post_update_migrate_photos_image_entity_1().
    \Drupal::database()
      ->schema()
      ->renameTable('photos_image', 'photos_image_tmp');

    // Rename {photos_album} fields.
    $album_id_field = [
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'pid', 'album_id', $album_id_field);
    $cover_id_field = [
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'fid', 'cover_id', $cover_id_field);
    $weight_field = [
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ];
    \Drupal::database()
      ->schema()
      ->changeField('photos_album', 'wid', 'weight', $weight_field);

    // Create new photos_image entity.
    $definition_update_manager = Drupal::entityDefinitionUpdateManager();

    // Basically the entity plugin definition for the custom entity, converted
    // to an associative array.
    $entity_definition = new ContentEntityType([
      "id" => "photos_image",
      "label" => new TranslatableMarkup("Photo"),
      "label_collection" => new TranslatableMarkup("Photos"),
      "label_singular" => new TranslatableMarkup("photo"),
      "label_plural" => new TranslatableMarkup("photos"),
      "label_count" => new PluralTranslation([
        "singular" => "@count photo",
        "plural" => "@count photos",
      ]),
      "handlers" => [
        "storage" => "Drupal\\photos\\PhotosImageStorage",
        "storage_schema" => "Drupal\\photos\\PhotosImageStorageSchema",
        "form" => [
          "default" => "Drupal\\photos\\Form\\PhotosImageEditForm",
          "add" => "Drupal\\photos\\Form\\PhotosImageAddForm",
          "edit" => "Drupal\\photos\\Form\\PhotosImageEditForm",
          "delete" => "Drupal\\photos\\Form\\PhotosImageDeleteForm",
          "delete-multiple-confirm" => "Drupal\\Core\\Entity\\Form\\DeleteMultipleForm",
        ],
        "access" => "Drupal\\photos\\PhotosAccessControlHandler",
        "views_data" => "Drupal\\photos\\PhotosViewsData",
        "list_builder" => "Drupal\\photos\\PhotosImageListBuilder",
        "route_provider" => [
          "html" => "Drupal\\photos\\Entity\\PhotosRouteProvider",
        ],
      ],
      "base_table" => "photos_image",
      "data_table" => "photos_image_field_data",
      "revision_table" => "photos_image_revision",
      "revision_data_table" => "photos_image_field_revision",
      "translatable" => TRUE,
      "show_revision_ui" => TRUE,
      "entity_keys" => [
        "id" => "id",
        "revision" => "revision_id",
        "label" => "title",
        "langcode" => "langcode",
        "uuid" => "uuid",
        "status" => "status",
        "published" => "status",
        "uid" => "uid",
        "owner" => "uid",
      ],
      "revision_metadata_keys" => [
        "revision_user" => "revision_user",
        "revision_created" => "revision_created",
        "revision_log_message" => "revision_log_message",
      ],
      "admin_permission" => "administer nodes",
      "common_reference_target" => TRUE,
      "field_ui_base_route" => "photos.admin",
      "links" => [
        "canonical" => "/photos/{node}/{photos_image}",
        "add-form" => "/photos/image/add",
        "delete-form" => "/photos/{node}/{photos_image}/delete",
        "edit-form" => "/photos/{node}/{photos_image}/edit",
        "version-history" => "/photos/{node}/{photos_image}/revisions",
        "revision" => "/photos/{node}/{photos_image}/revisions/{photos_image_revision}/view",
      ],
    ]);

    // Finally you need to manually set the class to where the entity plugin
    // definition is.
    $entity_definition
      ->setClass('Drupal\\photos\\Entity\\PhotosImage');

    // Create new entity type.
    $definition_update_manager
      ->installEntityType($entity_definition);

    // Add photos_image field.
    // @todo update field path settings based on existing path settings.
    // @todo replace old field path tokens with new tokens.
    // @todo add field display and form display.
    $field_storage = FieldStorageConfig::loadByName('photos_image', 'field_image');
    $field = FieldConfig::loadByName('photos_image', 'photos_image', 'field_image');
    if (empty($field)) {
      if (empty($field_storage)) {
        $field_storage = FieldStorageConfig::create([
          'entity_type' => 'photos_image',
          'field_name' => 'field_image',
          'type' => 'image',
          'settings' => [
            'target_type' => 'file',
          ],
          'cardinality' => 1,
        ]);
        $field_storage
          ->save();
      }
      $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'photos_image',
        'label' => 'Image',
        'settings' => [
          'file_directory' => 'photos/images',
          'file_extensions' => 'png gif jpg jpeg',
        ],
      ]);
      $field
        ->save();
    }
  }
}

Functions

Namesort descending Description
photos_install Implements hook_install().
photos_schema Implements hook_schema().
photos_update_8501 Prep for new photos_image entity.