You are here

social_post_photo.module in Open Social 10.2.x

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';
  if ($form_state
    ->hasValue([
    'field_post_image',
    0,
    'fids',
    0,
  ])) {

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
    $display = $form_state
      ->get('form_display');
    $field = $display
      ->getComponent('field_post');
    $form['field_post']['widget'][0]['#placeholder'] = $field['settings']['placeholder'];
  }
}

/**
 * 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'])) {

    // At the moment there is no good alt text for photos in posts and the posts
    // are usually descriptive enough that the images can be considered
    // decorative. To properly indicate this we include an empty string as
    // alt-text. This is better than using the filename which is meaningless in
    // most cases. See Issue #2946442.
    $variables['image']['#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.