og-8.x-1.x-dev/og_ui/src/Form/OgRoleForm.php
og_ui/src/Form/OgRoleForm.php
<?php
declare(strict_types=1);
namespace Drupal\og_ui\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgRoleInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Form to add or edit an OG role.
*/
class OgRoleForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = '', $bundle_id = '') {
if ($this->getEntity()->isNew()) {
// Return a 404 error when this is not a group.
if (!Og::isGroup($entity_type_id, $bundle_id)) {
throw new NotFoundHttpException();
}
$this->getEntity()->setGroupType($entity_type_id);
$this->getEntity()->setGroupBundle($bundle_id);
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Role name'),
'#default_value' => $this->getEntity()->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
];
$form['name'] = [
'#type' => 'machine_name',
'#default_value' => $this->getEntity()->getName(),
'#required' => TRUE,
'#disabled' => !$this->getEntity()->isNew(),
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => [
'exists' => [$this, 'exists'],
],
'#field_prefix' => $this->getEntity()->getGroupType() . '-' . $this->getEntity()->getGroupBundle() . '-',
];
$form['weight'] = [
'#type' => 'value',
'#value' => $this->getEntity()->getWeight(),
];
$form['role_type'] = [
'#type' => 'value',
'#value' => OgRoleInterface::ROLE_TYPE_STANDARD,
];
return parent::buildForm($form, $form_state);
}
/**
* Machine name callback.
*
* Cannot use OgRole::load as the #machine_name callback as we are only
* allowing editing the role name.
*/
public function exists(string $role_name): ?OgRoleInterface {
return OgRole::getRole($this->getEntity()->getGroupType(), $this->getEntity()->getGroupBundle(), $role_name);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state): int {
// Prevent leading and trailing spaces in role names.
$this->getEntity()->set('label', trim($this->getEntity()->label()));
$this->getEntity()->set('name', trim($this->getEntity()->get('name')));
$status = $this->getEntity()->save();
$edit_link = $this->getEntity()->toLink($this->t('Edit'), 'edit-form')->toString();
if ($status == SAVED_UPDATED) {
$this->messenger()->addMessage($this->t('OG role %label has been updated.', ['%label' => $this->getEntity()->label()]));
$this->logger('user')->notice('OG role %label has been updated.', [
'%label' => $this->getEntity()->label(),
'link' => $edit_link,
]);
}
else {
$this->messenger()->addMessage($this->t('OG role %label has been added.', ['%label' => $this->getEntity()->label()]));
$this->logger('user')->notice('OG role %label has been added.', [
'%label' => $this->getEntity()->label(),
'link' => $edit_link,
]);
}
// Cannot use $this->getEntity()->toUrl() because we need to pass mandatory
// parameters.
$form_state->setRedirect('entity.og_role.collection', [
'entity_type_id' => $this->getEntity()->getGroupType(),
'bundle_id' => $this->getEntity()->getGroupBundle(),
]);
return $status;
}
/**
* Title callback for the edit form for an OG role.
*
* @param \Drupal\og\Entity\OgRole $og_role
* The OG role being edited.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* An object that, when cast to a string, returns the translated title
* callback.
*/
public function editRoleTitleCallback(OgRole $og_role): TranslatableMarkup {
return $this->t('Edit OG role %label', [
'%label' => $og_role->getLabel(),
]);
}
/**
* Returns the OG role entity.
*
* Extending the parent method for the sake of narrowing entity strict tying.
*
* @return \Drupal\og\OgRoleInterface
* The OG role entity.
*/
public function getEntity(): OgRoleInterface {
$og_role = $this->entity;
assert($og_role instanceof OgRoleInterface);
return $og_role;
}
}
