social_comment_upload.module in Open Social 8.2
Same filename and directory in other branches
- 8.9 modules/social_features/social_comment_upload/social_comment_upload.module
- 8 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.3 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.4 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.5 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.6 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.7 modules/social_features/social_comment_upload/social_comment_upload.module
- 8.8 modules/social_features/social_comment_upload/social_comment_upload.module
- 10.3.x modules/social_features/social_comment_upload/social_comment_upload.module
- 10.0.x modules/social_features/social_comment_upload/social_comment_upload.module
- 10.1.x modules/social_features/social_comment_upload/social_comment_upload.module
- 10.2.x modules/social_features/social_comment_upload/social_comment_upload.module
Module file for Social Comment Upload.
File
modules/social_features/social_comment_upload/social_comment_upload.moduleView source
<?php
/**
* @file
* Module file for Social Comment Upload.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
/**
* Implements hook_form_FORM_ID_alter().
*
* Alter the weight of the actions, so the upload is last.
*/
function social_comment_upload_form_comment_comment_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$settings = \Drupal::config('social_comment_upload.settings')
->get('allow_upload_comments');
if (\Drupal::config('social_comment_upload.settings')
->get('allow_upload_comments') == TRUE) {
if (isset($form['field_comment_files'])) {
$form['field_comment_files']['widget']['#file_upload_title'] = '';
$form['#attached']['library'][] = 'social_comment_upload/comment_upload';
$form['#after_build'][] = 'social_comment_upload_form_comment_comment_form_after_build';
// Workaround for fixing behat tests,
// which is caused by alter in social_comment module.
$form['field_comment_files']['#weight'] = 9;
$form['actions']['#weight'] = 8;
}
}
else {
// Turn it off.
$form['field_comment_files']['#access'] = FALSE;
}
}
/**
* Removes the select list with "weight" value.
*/
function social_comment_upload_form_comment_comment_form_after_build($form, FormStateInterface $form_state) {
foreach ($form['field_comment_files']['widget'] as &$element) {
if (is_array($element) && isset($element['_weight'])) {
unset($element['_weight']);
}
}
return $form;
}
/**
* Implements hook_preprocess_HOOK().
*/
function social_comment_upload_preprocess_file_widget_multiple(&$variables) {
if (\Drupal::config('social_comment_upload.settings')
->get('allow_upload_comments') == TRUE) {
$element = $variables['element'];
if ($element['#field_name'] == 'field_comment_files') {
// Remove "weight" column.
foreach ($variables['table']['#rows'] as $key => $row) {
if (!empty($element['#display_field'])) {
unset($variables['table']['#rows'][$key]['data'][2]);
}
else {
unset($variables['table']['#rows'][$key]['data'][1]);
}
}
// Remove headers and disable sorting rows.
$variables['table']['#header'] = [];
$variables['table']['#tabledrag'] = [];
}
}
}
/**
* Implements hook_comment_delete().
*/
function social_comment_upload_comment_delete(EntityInterface $entity) {
// If this comment has any files, delete those as well.
_social_comment_upload_delete_comment_files($entity);
}
/**
* Deletes the files associated with a comment.
*
* This can be used to clean up files that were uploaded with a comment once
* that comment is being removed.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The comment for which attached files should be deleted.
*/
function _social_comment_upload_delete_comment_files(EntityInterface $entity) {
// If this comment doesn't have any attached files we abort early.
if (!$entity
->hasField('field_comment_files') || $entity
->get('field_comment_files')
->isEmpty()) {
return;
}
foreach ($entity->field_comment_files as $file_reference) {
// Try to load the actual file object.
$file = File::load($file_reference->target_id);
// Check if it's an actual file object.
if ($file instanceof File) {
// Delete the file object.
$file
->delete();
}
}
}
Functions
Name![]() |
Description |
---|---|
social_comment_upload_comment_delete | Implements hook_comment_delete(). |
social_comment_upload_form_comment_comment_form_after_build | Removes the select list with "weight" value. |
social_comment_upload_form_comment_comment_form_alter | Implements hook_form_FORM_ID_alter(). |
social_comment_upload_preprocess_file_widget_multiple | Implements hook_preprocess_HOOK(). |
_social_comment_upload_delete_comment_files | Deletes the files associated with a comment. |