Preview:
<?php

/**
 * @file
 * Contains musin_core.module.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;

/**
 * Implements hook_views_data_alter().
 */
function musin_core_views_data_alter(array &$data) {

  $data['private_message_threads']['members_list'] = [
    'title' => t('Member(s) name list'),
    'help' => t('The member(s) name of the private message thread'),
    'field' => [
      'id' => 'musin_core_private_message_members_list',
    ],
  ];

  $data['private_message_threads']['members_pictures'] = [
    'title' => t('Member(s) pictures'),
    'help' => t('The member(s) pictures of the private message thread'),
    'field' => [
      'id' => 'musin_core_private_message_members_pictures',
    ],
  ];

  $data['private_message_threads']['thread_last_message'] = [
    'title' => t('Last message'),
    'help' => t('The last message of the private message thread'),
    'field' => [
      'id' => 'musin_core_private_message_thread_last_message',
    ],
  ];

  $data['private_message_threads']['thread_is_unread'] = [
    'title' => t('Is unread or not'),
    'help' => t('The last message is unreaded of the private message thread'),
    'field' => [
      'id' => 'musin_core_private_message_thread_is_unread',
    ],
  ];

}

/**
 * Implements hook_entity_extra_field_info().
 */
function musin_core_entity_extra_field_info() {
  $extra_field = [];

  $extra_field['private_message']['private_message']['display']['author_image'] = [
    'label' => t('Author image'),
    'description' => t('Author image.'),
    'weight' => 100,
    'visible' => TRUE,
  ];

  $extra_field['private_message']['private_message']['display']['author_name'] = [
    'label' => t('Author name'),
    'description' => t('Author name.'),
    'weight' => 100,
    'visible' => TRUE,
  ];

  return $extra_field;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function musin_core_private_message_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  if ($display->getComponent('author_image')) {

    /** @var \Drupal\musin_core\MessagesService $messages_service */
    $messages_service = \Drupal::service('musin_core.messages.service');

    $build['author_image'] = $messages_service->getUserPictureElement($entity->getOwner());

  }
  if ($display->getComponent('author_name')) {

    /** @var \Drupal\private_message\Entity\PrivateMessage $entity*/
    //$author_name = $entity->getOwner()->get('field_first_name')->value . ' ' . $entity->getOwner()->get('field_last_name')->value;
    $author_name = $entity->getOwner()->get('name')->value;

    $build['author_name'] = [
      '#type' => 'markup',
      '#markup' => $author_name,
    ];

  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function musin_core_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage('view');

  if ($view && $view['view']->id() == 'search') {
    $form['search_api_fulltext_1']['#prefix'] = '<div class="search-dropdown">';
    $form['field_genres']['#suffix'] = '</div>';
    $form['field_instruments']['#options']['All'] = 'Instruments';
    $form['field_genres']['#options']['All'] = 'Genres';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Redirect user to profile page after saving the profile
 */

function musin_core_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'user_form') {
    $form['#after_build'][] = '_musin_core_user_edit_change_password_label';
    $form['actions']['submit']['#submit'][] = 'musin_core_user_edit_form_submit';
  }

  if ($form_id === 'node_gig_edit_form') {
    $form['revision_information']['#access'] = FALSE;
  }
}

function _musin_core_user_edit_change_password_label($form, &$form_state) {
  //dsm($form['group_email_password_artist_name']['#groups']);
  $form['group_email_password_artist_name']['#groups']['group_email_password_artist_name'][0]['pass']['pass1']['#title'] = t('New password');
  $form['group_email_password_artist_name']['#groups']['group_email_password_artist_name'][0]['pass']['pass2']['#title'] = t('Confirm new password');

  return $form;
}


/**
 * Implements hook_field_widget_file_generic_form_alter().
 */
function musin_core_field_widget_file_generic_form_alter(&$element, FormStateInterface $form_state, $context) {
  if ($context['form']['#parents'][0] == "field_sample_tracks") {
    $element['#after_build'][] = '_set_description_field_required';
  }
}

function _set_description_field_required($element, FormStateInterface $form_state) {
  if (isset($element['description'])) {
    $element['description']['#required'] = TRUE;
  }
  return $element;
}

function musin_core_user_edit_form_submit($form, &$form_state) {
  $form_state->setRedirect('entity.user.canonical', ['user' => \Drupal::currentUser()->id()]);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function musin_core_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#submit'][] = 'musin_core_user_login_form_submit';
}

/**
 * Custom submit handler for the login form.
 */
function musin_core_user_login_form_submit($form, FormStateInterface $form_state) {
  $url = Url::fromRoute('<front>');
  $form_state->setRedirectUrl($url);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function musin_core_preprocess_region(&$variables) {
  if ($variables['region'] == 'primary_navigation') {
    $variables['#cache']['contexts'][] = 'user';
  }
}

/**
 * Implements hook_entity_presave().
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 * @param $term
 */
function musin_core_entity_presave(EntityInterface $entity) {
  if ($entity instanceof \Drupal\user\UserInterface) {
    $completeness = \Drupal\musin_core\Services\UserHelperService::getProfileCompleteness($entity);
    $entity->set('field_completeness', $completeness);
//    // Add typed in instrument to users profile view.
//    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
//      'field_typed_in_instrument' => 1
//    ]);
//    $term = reset($terms);
//    if ($term !== false) {
//      $entity->set('field_typed_in_instrument', $term);
//    }
  }


  // Moving typed in instruments to their vocabulary.
  if ($entity instanceof \Drupal\taxonomy\Entity\Term) {
    $name = $entity->get('name')->value;
    if ($entity->get('vid')->getValue()[0]['target_id'] == 'instruments' && $entity->get('field_typed_in_instrument')->value === 1) {
      $entity->set('vid', 'extra_instruments');
    } else if ($entity->get('vid')->getValue()[0]['target_id'] == 'genres' && $entity->get('field_typed_in_genre')->value === 1) {
      $entity->set('vid', 'extra_genres');
    }
  }

}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter