You are here

public function ExifSettingsController::createPhotographyNodeType in Exif 8

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

Create an Photography node type with default exif fields.

Default files are title, model, keywords.

Default behavior but 'promoted to front'.

Use by routing.yml

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

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

File

src/Controller/ExifSettingsController.php, line 109

Class

ExifSettingsController
Class ExifSettingsController manage action of settings pages.

Namespace

Drupal\exif\Controller

Code

public function createPhotographyNodeType() {
  $typeName = 'Photography';
  $entity_type = 'node';
  $machineName = strtolower($typeName);
  try {
    $storage = $this
      ->entityTypeManager()
      ->getStorage('node_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,
    ]);
  } 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;
}