You are here

public function ExifSettingsController::createPhotographyMediaType in Exif 8

Same name and namespace in other branches
  1. 8.2 src/Controller/ExifSettingsController.php \Drupal\exif\Controller\ExifSettingsController::createPhotographyMediaType()

Create a Photography node type with default exif field.

Default values are title, model, keywords. Default behavior but 'promoted to front'.

used by routing.yml

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 string reference to 'ExifSettingsController::createPhotographyMediaType'
exif.routing.yml in ./exif.routing.yml
exif.routing.yml

File

src/Controller/ExifSettingsController.php, line 177

Class

ExifSettingsController
Class ExifSettingsController manage action of settings pages.

Namespace

Drupal\exif\Controller

Code

public function createPhotographyMediaType() {
  $typeName = 'Photography';
  $entity_type = 'media';
  $machineName = strtolower($typeName);
  try {
    if (Drupal::moduleHandler()
      ->moduleExists("media_entity")) {
      $storage = $this
        ->entityTypeManager()
        ->getStorage($entity_type);
      $type_definition = $storage
        ->load($machineName);
      if (!$type_definition) {
        $type_definition = $storage
          ->create([
          'name' => $typeName,
          'type' => $machineName,
          'description' => 'Use Photography for content where the photo is the main content. You still can have some other information related to the photo itself.',
        ]);
        $type_definition
          ->save();
      }

      // Add default display.
      $values = [
        'targetEntityType' => $entity_type,
        'bundle' => $machineName,
        'mode' => 'default',
        'status' => TRUE,
      ];
      $this
        ->entityTypeManager()
        ->getStorage('entity_view_display')
        ->create($values);

      // Add default form display.
      $values = [
        'targetEntityType' => $entity_type,
        'bundle' => $machineName,
        'mode' => 'default',
        'status' => TRUE,
      ];
      $this
        ->entityTypeManager()
        ->getStorage('entity_form_display')
        ->create($values);

      // Then add fields.
      $this
        ->addFields($entity_type, $type_definition);
      $message = $this
        ->t('The %entitytype type %type has been fully created', [
        '%entitytype' => $entity_type,
        '%type' => $typeName,
      ]);
    }
    else {
      $message = 'Nothing done. Media modules not present.';
    }
  } catch (FieldException $fe) {
    $message = $this
      ->t('An unexpected error was thrown during creation : ') . $fe
      ->getMessage();
  }
  drupal_set_message($message);
  $response = new RedirectResponse('/admin/config/media/exif/helper');
  $response
    ->send();
  exit;
}