You are here

mailhandler_demo.install in Mailhandler 8

Implementations of install hooks for Mailhandler Demo module.

File

mailhandler_demo/mailhandler_demo.install
View source
<?php

/**
 * @file
 * Implementations of install hooks for Mailhandler Demo module.
 */
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;

/**
 * Implements hook_install().
 */
function mailhandler_demo_install() {

  // Create "mailhandler" content type.
  if (!($mailhandler_node_type = NodeType::load('mailhandler'))) {
    $mailhandler_node_type = NodeType::create([
      'type' => 'mailhandler',
      'name' => t('Mailhandler'),
      'description' => t('Demo content type created to test Mailhandler module.'),
    ]);
    $mailhandler_node_type
      ->save();
  }
  node_add_body_field($mailhandler_node_type);

  // Create "mailhandler" role.
  if (!($role = Role::load('mailhandler'))) {
    $role = Role::create([
      'id' => 'mailhandler',
      'label' => 'Mailhandler',
    ]);
  }
  $role
    ->grantPermission('create mailhandler content');
  $role
    ->grantPermission('post comments');
  $role
    ->save();

  // Update the user form display.
  entity_get_form_display('user', 'user', 'default')
    ->setComponent('mailhandler_gpg_key', [
    'type' => 'mailhandler_gpg',
    'weight' => 5,
    'settings' => [
      'rows' => 20,
      'placeholder' => 'Begins with "-----BEGIN PGP PUBLIC KEY BLOCK-----"',
    ],
    'third_party_settings' => [],
  ])
    ->save();

  // Update user display with GPG key.
  entity_get_display('user', 'user', 'default')
    ->setComponent('mailhandler_gpg_key', [
    'type' => 'mailhandler_gpg',
    'weight' => 2,
    'label' => 'above',
    'settings' => [
      'display' => 'fingerprint',
    ],
    'third_party_settings' => [],
  ])
    ->save();

  // Get a demo public key.
  $path = drupal_get_path('module', 'mailhandler_demo') . '/keys/public.key';
  $public_key = file_get_contents(DRUPAL_ROOT . '/' . $path);
  $entity_manager = \Drupal::entityManager();

  // Create "mailhandler" user.
  $users = $entity_manager
    ->getStorage('user')
    ->loadByProperties([
    'mail' => 'demo@example.com',
  ]);
  if (!($demo_user = reset($users))) {
    $demo_user = User::create([
      'mail' => 'demo@example.com',
      'name' => 'Demo User',
      'status' => TRUE,
      'mailhandler_gpg_key' => [
        'public_key' => $public_key,
        'fingerprint' => '266B764825A210EE327CE70F7396A4ED5F5EED56',
      ],
    ]);
  }
  $demo_user
    ->addRole($role
    ->id());
  $demo_user
    ->save();

  // Create a demo node.
  $demo_node = Node::create([
    'title' => t('Mailhandler Demo'),
    'type' => $mailhandler_node_type
      ->id(),
    'uid' => $demo_user
      ->id(),
    'body' => [
      'value' => '<p><a href="http://drupal.org/project/mailhandler">Mailhandler</a> is a <a href="https://www.drupal.org/8">Drupal 8</a> module that allows you to post nodes by email.</p>' . '<p>It was developed as a part of <a href="https://summerofcode.withgoogle.com/projects/#4520809229975552">Google Summer of Code 2016</a>, following the <a href="https://www.drupal.org/node/2731519">road map</a> and motivated by <a href="https://www.drupal.org/project/mailhandler">Mailhandler for Drupal 7</a>.</p>' . '<p>The Drupal 8 version of the module is based on <a href="https://www.drupal.org/project/inmail">Inmail</a> module and takes mail (usually from an IMAP mailbox) and imports it as whatever type of content you choose. Beside the configured content type, you can specify the content type directly in your email subject. It enables you to select different authentication methods and mail analyzers as well.</p>' . '<p>Mailhandler Comment is a submodule that allows you to post comments by email.</p>' . '<p>Hugely powerful and flexible, Mailhandler includes a demo module to help you get started.</p>' . '<h1>Installation</h1>' . '<p>To use this module you will need to have:</p>' . '<ul>' . '<li>Installed <a href="https://www.drupal.org/project/drupal">Drupal 8 Core</a></li>' . '<li><a href="https://www.drupal.org/project/inmail">Inmail</a></li>' . '<li><a href="https://www.drupal.org/project/mailhandler">Mailhandler</a> (Make sure you choose 8.x version of the module)</li>' . '<li>Recommended: <a href="http://php.net/manual/en/gnupg.setup.php">GnuPG PHP extension</a> (Support for PGP-signed emails)</li>' . '</ul>' . '<p>Take a look at the quick demo video that explains Mailhandler workflow: </p>' . '<p><iframe allowfullscreen="" frameborder="0" height="366" mozallowfullscreen="" src="https://player.vimeo.com/video/175383067" webkitallowfullscreen="" width="640"></iframe></p>' . '<p><a href="https://vimeo.com/175383067">Drupal 8 module demo: Mailhandler</a> from <a href="https://vimeo.com/user54537495">Milos Bovan</a> on <a href="https://vimeo.com">Vimeo</a>.</p>' . '<h1>How-To</h1>' . '<ul>' . '<li>What is the needed email format for Mailhandler?</li>' . '</ul>' . '<p>All emails parsed by Mailhandler need to have a subject that begins with <code>[node][{content_type}]</code> (for nodes) or <code>[comment][{#entity_ID}]</code> (for comments). The first parameters is an entity type ID while the second one is a content type or an entity ID. Both parameters needs to be valid.</p>' . '<h1>Contributing</h1>' . '<p>The preferred way to contribute is to create an issue on the <a href="https://www.drupal.org/project/issues/mailhandler?version=8.x">project page</a>.</p>' . '<p>However, if you are more into Github contribution, you can submit a pull request too.</p>' . '<ul>' . '<li><a href="https://github.com/fantastic91/mailhandler">Fork it!</a></li>' . '<li>Create your feature branch: <code>git checkout -b my-new-feature</code></li>' . '<li>Commit your changes: <code>git commit -am \'Add some feature\'</code></li>' . '<li>Push to the branch: <code>git push origin my-new-feature</code></li>' . '<li>Submit a pull request</li>' . '</ul>' . '<h1>Credits</h1>' . '<p>This project has been developed as a part of <a href="https://summerofcode.withgoogle.com/projects/#4520809229975552">Google Summer of Code 2016</a> by <a href="https://www.drupal.org/u/mbovan">Miloš Bovan</a> and mentored by <a href="https://www.drupal.org/u/miro_dietiker">Miro Dietiker</a> and <a href="https://www.drupal.org/u/Primsi">Primož Hmeljak</a>.</p>' . '<h1>License</h1>' . '<p><a href="https://www.gnu.org/licenses/gpl-2.0.html">https://www.gnu.org/licenses/gpl-2.0.html</a></p>',
      'format' => 'full_html',
    ],
  ]);
  $demo_node
    ->save();

  // Set the demo node as the front page.
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.front', '/node/' . $demo_node
    ->id())
    ->save();

  // Add comment field to mailhandler content type.
  // Create the comment type if needed.
  $comment_type_storage = $entity_manager
    ->getStorage('comment_type');
  if (!($comment_type = $comment_type_storage
    ->load('comment'))) {
    $comment_type_storage
      ->create([
      'id' => 'comment',
      'label' => Unicode::ucfirst('comment'),
      'target_entity_type_id' => 'node',
      'description' => 'Default comment field',
    ])
      ->save();
  }

  // Add a body field to the comment type.
  \Drupal::service('comment.manager')
    ->addBodyField('comment');

  // Add a comment field to the host entity type. Create the field storage if
  // needed.
  if (!array_key_exists('comment', $entity_manager
    ->getFieldStorageDefinitions('node'))) {
    $entity_manager
      ->getStorage('field_storage_config')
      ->create([
      'entity_type' => 'node',
      'field_name' => 'comment',
      'type' => 'comment',
      'translatable' => TRUE,
      'settings' => [
        'comment_type' => 'comment',
      ],
    ])
      ->save();
  }

  // Create the field if needed, and configure its form and view displays.
  if (!array_key_exists('comment', $entity_manager
    ->getFieldDefinitions('node', 'mailhandler'))) {
    $entity_manager
      ->getStorage('field_config')
      ->create([
      'label' => 'Comments',
      'description' => '',
      'field_name' => 'comment',
      'entity_type' => 'node',
      'bundle' => 'mailhandler',
      'required' => 1,
      'default_value' => [
        [
          'status' => CommentItemInterface::OPEN,
          'cid' => 0,
          'last_comment_name' => '',
          'last_comment_timestamp' => 0,
          'last_comment_uid' => 0,
        ],
      ],
    ])
      ->save();

    // Entity form displays: assign widget settings for the 'default' form
    // mode, and hide the field in all other form modes.
    entity_get_form_display('node', 'mailhandler', 'default')
      ->setComponent('comment', array(
      'type' => 'comment_default',
      'weight' => 102,
    ))
      ->save();

    // Entity view displays: assign widget settings for the 'default' view
    // mode, and hide the field in all other view modes.
    entity_get_display('node', 'mailhandler', 'default')
      ->setComponent('comment', [
      'label' => 'above',
      'type' => 'comment_default',
      'weight' => 102,
    ])
      ->save();
  }
}

Functions

Namesort descending Description
mailhandler_demo_install Implements hook_install().