You are here

function photos_access_node_validate in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 photos_access/photos_access.module \photos_access_node_validate()
  2. 7.3 photos_access/photos_access.module \photos_access_node_validate()
  3. 6.0.x photos_access/photos_access.module \photos_access_node_validate()

D7 - Implements hook_node_validate().

@todo: Remove this in favor of entity field API.

1 string reference to 'photos_access_node_validate'
photos_access_form_node_form_alter in photos_access/photos_access.module
Implements hook_form_BASE_FORM_ID_alter() for form_node.

File

photos_access/photos_access.module, line 378
Implementation of photos_access.module.

Code

function photos_access_node_validate(&$form, FormStateInterface $form_state) {

  // Old values.
  // $node = $form_state->getFormObject()->getEntity();
  // Check if users can be added to list.
  $privacy = $form_state
    ->getValue('privacy');
  $access_id = $privacy['access_id'];
  if ($access_id) {

    // @todo handle all of this on submit.
    if (isset($privacy['updateuser']) && !empty($privacy['updateuser'])) {
      if ($collaborators = _photos_access_user_validate($privacy['updateuser'], $access_id, TRUE)) {
        $form_state
          ->setErrorByName('privacy][updateuser', $collaborators);
      }
    }
    if (isset($privacy['viewid']) && $privacy['viewid'] == 2) {
      if (isset($privacy['viewuser']) && !empty($privacy['viewuser'])) {
        if ($designated = _photos_access_user_validate($privacy['viewuser'], $access_id)) {
          $form_state
            ->setErrorByName('privacy][viewuser', $designated);
        }
      }
    }
  }

  // Check if password is empty.
  if (isset($privacy['viewid']) && $privacy['viewid'] == 3) {
    if (isset($privacy['pass']) && empty($privacy['pass'])) {

      // Check if current password is set.
      $db = \Drupal::database();
      $current_pass = $db
        ->query("SELECT pass FROM {photos_access_album} WHERE id = :id", [
        ':id' => $access_id,
      ])
        ->fetchField();
      if (!$current_pass) {
        $form_state
          ->setErrorByName('privacy][pass', t('Password required.'));
      }
    }
  }
}