You are here

function twitter_profile_widget_entity_insert in Twitter Profile Widget 8

Implements hook_entity_insert().

Upon initial creation of a Twitter widget entity, create a corresponding block, using the widget label (name) as the block info (description).

File

./twitter_profile_widget.module, line 50
Contains twitter_profile_widget.module.

Code

function twitter_profile_widget_entity_insert(EntityInterface $entity) {

  // Only act on twitter_widget entities.
  if ($entity
    ->getEntityTypeId() == 'twitter_widget' && $entity
    ->bundle() == 'twitter_widget') {
    $config = \Drupal::config('twitter_profile_widget.settings');
    if ($config
      ->get('integrate_blocks')) {
      $blockEntityManager = \Drupal::service('entity.manager')
        ->getStorage('block_content');

      // Create a block of type "twitter_widget".
      $block = $blockEntityManager
        ->create([
        'type' => 'twitter_widget',
      ]);

      // Every block should have a description; strangely its property is not
      // 'description' but 'info'.
      $block->info = $entity
        ->get('name')->value;

      // Assign the twitter widget entity to the block's field_twitter_widget
      // entity reference field, effectively making the widget a block.
      $block->field_twitter_widget = $entity;
      $block
        ->save();
      \Drupal::logger('twitter_profile_widget')
        ->notice('Twitter block "%name" created.', [
        '%name' => $entity
          ->get('name')->value,
      ]);
    }
  }
}