SocialAlbumPostForm.php in Open Social 10.2.x
File
modules/social_features/social_album/src/Form/SocialAlbumPostForm.php
View source
<?php
namespace Drupal\social_album\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\social_post\Form\PostForm;
class SocialAlbumPostForm extends PostForm {
const VISIBILITY_MAPPING = [
'public' => '1',
'community' => '2',
'group' => '3',
];
protected $node;
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$this->node = $node;
$form = parent::buildForm($form, $form_state);
if ($node) {
$form['#disable_inline_form_errors'] = TRUE;
$form['field_album']['widget']['value']['#default_value'] = $node
->id();
$form['field_album']['#access'] = FALSE;
$form['field_post_image']['widget'][0]['#required'] = TRUE;
}
return $form;
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$element = parent::actionsElement($form, $form_state);
if ($this->node) {
$element['cancel'] = $this->node
->toLink($this
->t('Go to album'))
->toRenderable();
}
return $element;
}
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
if ($this->node) {
$form_state
->setRedirect('entity.node.canonical', [
'node' => $this->node
->id(),
]);
}
}
protected function configureVisibilityField(array &$form, FormStateInterface $form_state) {
parent::configureVisibilityField($form, $form_state);
if ($this->node) {
$field =& $form['field_visibility']['widget'][0];
$value = self::VISIBILITY_MAPPING[$this->node->field_content_visibility->value];
if (isset($field['#options'][$value])) {
$field['#default_value'] = $value;
}
$field['#edit_mode'] = TRUE;
}
}
}