class EntityForm in Acquia Content Hub 8
Defines a form that alters entity form to add a Content Hub form.
Hierarchy
- class \Drupal\acquia_contenthub\Form\EntityForm uses StringTranslationTrait
Expanded class hierarchy of EntityForm
1 file declares its use of EntityForm
- EntityFormTest.php in tests/
src/ Unit/ Form/ EntityFormTest.php
1 string reference to 'EntityForm'
1 service uses EntityForm
File
- src/
Form/ EntityForm.php, line 14
Namespace
Drupal\acquia_contenthub\FormView source
class EntityForm {
use StringTranslationTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
private $currentUser;
/**
* The Content Hub Entities Tracking Service.
*
* @var \Drupal\acquia_contenthub\ContentHubEntitiesTracking
*/
private $contentHubEntitiesTracking;
/**
* Constructor.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\acquia_contenthub\ContentHubEntitiesTracking $entities_tracking
* The Content Hub Entities Tracking Service.
*/
public function __construct(AccountInterface $current_user, ContentHubEntitiesTracking $entities_tracking) {
$this->currentUser = $current_user;
$this->contentHubEntitiesTracking = $entities_tracking;
}
/**
* Get Form.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return array
* Acquia Content Hub Entity Form.
*/
public function getForm(EntityInterface $entity) {
// Don't display anything if the entity doesn't exist.
$entity_id = $entity
->id();
if (empty($entity_id)) {
return NULL;
}
$imported_entity = $this->contentHubEntitiesTracking
->loadImportedByDrupalEntity($entity
->getEntityTypeId(), $entity_id);
// If the entity is not imported, do not display form.
if (!$imported_entity) {
return NULL;
}
$form = [
'#type' => 'details',
'#title' => $this
->t('Acquia Content Hub settings'),
'#access' => $this->currentUser
->hasPermission('administer acquia content hub'),
'#group' => 'advanced',
'#tree' => TRUE,
'#weight' => 30,
];
$has_local_change = $imported_entity
->hasLocalChange();
$form['auto_update_label'] = [
'#type' => 'markup',
'#markup' => $has_local_change ? $this
->t('This syndicated content has been modified locally, therefore it is no longer automatically synchronized to its original content.') : $this
->t('This is a syndicated content. What happens if its original content is updated?'),
];
$form['auto_update'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable this syndicated content to receive the same updates of its original content'),
'#default_value' => $imported_entity
->isAutoUpdate(),
];
if ($has_local_change) {
$form['auto_update_local_changes_label'] = [
'#type' => 'markup',
'#markup' => '<div>' . $this
->t('Check to enable synchronization with any future updates of content from Content Hub.') . '</div><div><strong>' . $this
->t("Any edits that were made to your site's instance of this content will be overwritten by the Content Hub version.") . '</strong></div>',
];
}
return $form;
}
/**
* Attach submit handler to form actions.
*
* @param array &$form_actions
* Form actions.
* @param string $submit_handler_name
* Submit handler's name.
*/
public function attachSubmitHandler(array &$form_actions, $submit_handler_name) {
foreach (array_keys($form_actions) as $action) {
if ($action === 'preview' || !isset($form_actions[$action]['#type']) || $form_actions[$action]['#type'] !== 'submit') {
continue;
}
array_unshift($form_actions[$action]['#submit'], $submit_handler_name);
}
}
/**
* Save settings.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
public function saveSettings(FormStateInterface $form_state) {
if ($form_state
->isValueEmpty('acquia_contenthub')) {
return;
}
$entity = $form_state
->getFormObject()
->getEntity();
$imported_entity = $this->contentHubEntitiesTracking
->loadImportedByDrupalEntity($entity
->getEntityTypeId(), $entity
->id());
if (!$imported_entity) {
return;
}
$values = $form_state
->getValue('acquia_contenthub');
$new_auto_update_flag = $values['auto_update'];
// If we are changing state to "sync enabled" (from anything else),
// set state to resync entity. Otherwise, just set the new autoUpdate flag.
$set_pending_sync = !$imported_entity
->isAutoUpdate() && $new_auto_update_flag === 1;
$set_pending_sync ? $imported_entity
->setPendingSync() : $imported_entity
->setAutoUpdate($new_auto_update_flag);
$imported_entity
->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityForm:: |
private | property | The Content Hub Entities Tracking Service. | |
EntityForm:: |
private | property | The current user. | |
EntityForm:: |
public | function | Attach submit handler to form actions. | |
EntityForm:: |
public | function | Get Form. | |
EntityForm:: |
public | function | Save settings. | |
EntityForm:: |
public | function | Constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |