You are here

function social_core_form_node_form_alter in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  2. 8 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  3. 8.2 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  4. 8.3 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  5. 8.4 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  6. 8.5 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  7. 8.6 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  8. 8.7 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  9. 8.8 modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  10. 10.3.x modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  11. 10.0.x modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()
  12. 10.2.x modules/social_features/social_core/social_core.module \social_core_form_node_form_alter()

Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_core/social_core.module, line 745
The Social core module.

Code

function social_core_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Array of node types, which have the new group_settings fieldset,
  // these nodes can use the new content style, and have $form['advanced']
  // removed.
  // if we remove it for nodes which don't have that fieldset, fields will be
  // rendered outside of a card.
  $compatible_content_type_forms = [
    'node_event_form',
    'node_event_edit_form',
    'node_topic_form',
    'node_topic_edit_form',
    'node_page_form',
    'node_page_edit_form',
  ];

  // Create new alter hook which allows extensions to add nodes
  // to this list once they have added the group_settings.
  \Drupal::moduleHandler()
    ->alter('social_core_compatible_content_forms', $compatible_content_type_forms);

  // Let's remove the form elements not needed by Open Social.
  // We understand this is quite BC breaking, url_redirects / revision info
  // we don't use as Open Social, you might use. So by
  // changing the theme setting in social blue, you can go back to the
  // old settings and use Field UI to change the other fields.
  $use_social_content_forms = theme_get_setting('content_entity_form_style');
  if ($use_social_content_forms === 'open_social' && in_array($form_id, $compatible_content_type_forms)) {

    // We want to move all the fields from $form['advanced'] to our new
    // field sets which are created as part of #3186003.
    if (!empty($form['advanced'])) {
      unset($form['advanced']);

      // Unset optional fields.
      if (isset($form['revision'])) {
        unset($form['revision']);
      }
      if (isset($form['revision_log'])) {
        unset($form['revision_log']);
      }
      if (isset($form['revision_information'])) {
        unset($form['revision_information']);
      }
      if (isset($form['url_redirects'])) {
        unset($form['url_redirects']);
      }
      if (isset($form['meta'])) {
        unset($form['meta']);
      }

      // Add descriptive text to URL Alias field.
      if (isset($form['path']['widget'][0]['alias'])) {
        $form['path']['widget'][0]['alias']['#description'] = t('The URL alias allows you to customise the link to this page.');
      }

      // Hide the automatic URL alias.
      if (isset($form['path']['widget'][0]['pathauto'])) {
        $form['path']['widget'][0]['pathauto']['#type'] = 'hidden';

        // Path uses the above checkbox to decide whether or not to save
        // the automatically generated alias, or use a custom one.
        // since we hide the element, and the default is checked,
        // the path will never be overridden if someone updates it.
        // So we need to check this in our custom submit handler.
        // It should perform before path auto runs.
        array_unshift($form['actions']['submit']['#submit'], '_social_core_path_widget_submit');
      }

      // Move the fields from advanced to group_settings.
      if (!empty($form['#fieldgroups']['group_settings'])) {

        // Move the book outline fields to the group settings.
        // #502430 we need to remove this ourselves, users with permission
        // can see the book outline even when not configured as part
        // of the book settings.
        if (!empty($form['book']) && $form_state
          ->getFormObject() !== NULL && \Drupal::moduleHandler()
          ->moduleExists('book')) {

          // Grab the Entity to see if books are enabled for this one.
          $entity = $form_state
            ->getFormObject()
            ->getEntity();
          if (!empty($entity
            ->getType()) && is_string($entity
            ->getType())) {

            // Move it to our new fieldset on book pages either on its own
            // or in the group_settings.
            $form['book']['#type'] = 'fieldset';
            $form['book']['#attributes']['class'][] = 'card';
            $form['book']['#weight'] = isset($form['#fieldgroups']['group_visibility']->weight) ? $form['#fieldgroups']['group_visibility']->weight + 1 : '3';
            $enabled = book_type_is_allowed($entity
              ->getType());

            // If it's not enabled we can just safely unset it.
            if (!$enabled) {
              unset($form['book']);
            }
          }
        }

        // Move the node Menu settings to group_settings.
        if (!empty($form['menu'])) {
          $form['menu']['#group'] = 'group_settings';
          $form['menu']['#weight'] = -20;
          $form['menu']['#title'] = t('Menu link');
          if (!empty($form['menu']['link']['title'])) {
            $form['menu']['link']['title']['#title'] = t('Title');
            $form['menu']['link']['title']['#description'] = t('This title will be displayed in the menu.');
          }
          if (!empty($form['menu']['link']['description'])) {
            $form['menu']['link']['title']['#description'] = t('This description will be displayed when hovering over the menu link.');
          }
        }

        // Move the Published status to group settings with a new wrapper.
        if (!empty($form['status'])) {

          // Add an details element to status.
          $form['status_label'] = [
            '#type' => 'details',
            '#title' => t('Publish status'),
            '#description' => '',
            '#open' => TRUE,
            '#weight' => 110,
            '#group' => 'group_settings',
          ];
          $form['status']['#group'] = 'status_label';
          $form['status_label']['status'] = $form['status'];
          unset($form['status']);
        }

        // Node author information for administrators.
        $form['author_info'] = [
          '#type' => 'details',
          '#title' => t('Author Information'),
          '#description' => '',
          '#open' => TRUE,
          '#weight' => 100,
          '#group' => 'group_settings',
        ];

        // Move author uid to the new author info fieldset,
        // and remove descriptive text.
        if (isset($form['uid'])) {
          $form['uid']['#group'] = 'author_info';
          $form['uid']['widget']['#description'] = '';
          if (!empty($form['uid']['widget'][0]['target_id'])) {
            $form['uid']['widget'][0]['target_id']['#description'] = '';
          }
          $form['author_info']['uid'] = $form['uid'];
          unset($form['uid']);
        }

        // Move created to the new author info fieldset,
        // and remove descriptive text.
        if (isset($form['created'])) {
          $form['created']['#group'] = 'author_info';
          $form['created']['widget']['#description'] = '';
          $form['created']['widget'][0]['#description'] = '';
          $form['created']['widget'][0]['value']['#description'] = '';
          $form['author_info']['created'] = $form['created'];
          unset($form['created']);
        }
        $has_option_fields = FALSE;
        foreach ([
          'promote',
          'sticky',
        ] as $field) {
          if (isset($form[$field])) {
            $form['options'][$field] = $form[$field];
            unset($form[$field]);
            $has_option_fields = TRUE;
          }
        }

        // When the "Promoted to front page" field or the "Sticky at top of
        // lists" field is enabled then move their wrapper (the "Promotion
        // options" sub-section) to the "Settings" section.
        // @see \Drupal\node\NodeForm::form().
        if ($has_option_fields) {
          $form['options']['#group'] = 'group_settings';

          // Place the "Promotion options" sub-section after the "Publish
          // status" sub-section.
          $form['options']['#weight'] = 120;

          // Display sub-section as expanded.
          $form['options']['#optional'] = FALSE;
        }
      }

      // According to FileWidget.php we cant use a widget form alter.
      if (!empty($form['field_files']) && !empty($form['field_files']['widget']['#file_upload_title'])) {
        $form['field_files']['widget']['#file_upload_title'] = t('Attachments');
      }
    }

    // Change the button text from Save to Create topic / event etc.
    // Adds a cancel button.
    // Removes the preview button.
    if (!empty($form['actions']['submit']) && !empty($form['actions']['submit']['#value']) && $form_state
      ->getFormObject() !== NULL && method_exists($form_state
      ->getFormObject(), 'getOperation')) {

      // Change the button text from Save to Create topic / event etc.
      // Only on node add pages.
      if ($form_state
        ->getFormObject()
        ->getOperation() !== 'edit') {

        /** @var \Drupal\Node\NodeInterface $entity */
        $entity = $form_state
          ->getFormObject()
          ->getEntity();
        if (!empty(node_get_type_label($entity)) && is_string(node_get_type_label($entity))) {
          $form['actions']['submit']['#value'] = t('Create @entity', [
            '@entity' => strtolower(node_get_type_label($entity)),
          ]);
        }
      }

      // Adds cancel button.
      if (!isset($form['actions']['cancel'])) {
        $form['actions']['cancel'] = [
          '#type' => 'submit',
          '#value' => t('Cancel'),
          '#submit' => [
            '_social_core_node_cancel',
          ],
          '#limit_validation_errors' => [],
          '#attributes' => [
            'class' => [
              'button btn-default btn waves-effect waves-btn',
            ],
          ],
        ];
      }

      // Remove the preview button.
      if (isset($form['actions']['preview'])) {
        unset($form['actions']['preview']);
      }
    }
  }
}