You are here

social_post_photo.module in Open Social 8.2

The Social post photo module.

File

modules/social_features/social_post/modules/social_post_photo/social_post_photo.module
View source
<?php

/**
 * @file
 * The Social post photo module.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function social_post_photo_form_post_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Don't show a title nor a description.
  $form['field_post_image']['widget'][0]['#title'] = '';
  $form['field_post_image']['widget'][0]['#description'] = '';

  // Add JS to change the placeholder. @TODO: Improve this.
  $form['#attached']['library'][] = 'social_post_photo/widget';
}

/**
 * Implements hook_photo_preprocess_image_formatter().
 */
function social_post_photo_preprocess_image_formatter(&$variables) {
  if ($variables['image_style'] == "social_post_photo" && empty($variables['image']['#alt'])) {

    // Set the image alt to the filename.
    $uri = $variables['image']['#uri'];
    $alt = substr(strrchr($uri, "/"), 1);
    $variables['image']['#alt'] = $alt;
  }
}

/**
 * Function to change the post type.
 *
 * @param string $from
 *   Type to change it from.
 * @param string $to
 *   Type to change it to.
 */
function _social_post_photo_settype($from, $to) {

  // Get post storage.
  $poststorage = \Drupal::entityTypeManager()
    ->getStorage('post');

  // Load all posts of type post.
  $posts = $poststorage
    ->loadByProperties([
    'type' => $from,
  ]);

  /** @var \Drupal\social_post\Entity\Post $post */
  foreach ($posts as $post) {
    $post
      ->set('type', $to)
      ->save();
  }
}

Functions

Namesort descending Description
social_post_photo_form_post_form_alter Implements hook_form_FORM_ID_alter().
social_post_photo_preprocess_image_formatter Implements hook_photo_preprocess_image_formatter().
_social_post_photo_settype Function to change the post type.