Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public static function valueCallback(&$element, $input, FormStateInterface $form_state)
 {
     // Find the current value of this field.
     $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
     foreach ($fids as $key => $fid) {
         $fids[$key] = (int) $fid;
     }
     // Process any input and save new uploads.
     if ($input !== FALSE) {
         $input['fids'] = $fids;
         $return = $input;
         // Uploads take priority over all other values.
         if ($files = file_managed_file_save_upload($element, $form_state)) {
             if ($element['#multiple']) {
                 $fids = array_merge($fids, array_keys($files));
             } else {
                 $fids = array_keys($files);
             }
         } else {
             // Check for #filefield_value_callback values.
             // Because FAPI does not allow multiple #value_callback values like it
             // does for #element_validate and #process, this fills the missing
             // functionality to allow File fields to be extended through FAPI.
             if (isset($element['#file_value_callbacks'])) {
                 foreach ($element['#file_value_callbacks'] as $callback) {
                     $callback($element, $input, $form_state);
                 }
             }
             // Load files if the FIDs have changed to confirm they exist.
             if (!empty($input['fids'])) {
                 $fids = [];
                 foreach ($input['fids'] as $fid) {
                     if ($file = File::load($fid)) {
                         $fids[] = $file->id();
                     }
                 }
             }
         }
     } else {
         if ($element['#extended']) {
             $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : [];
             $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []];
         } else {
             $default_fids = isset($element['#default_value']) ? $element['#default_value'] : [];
             $return = ['fids' => []];
         }
         // Confirm that the file exists when used as a default value.
         if (!empty($default_fids)) {
             $fids = [];
             foreach ($default_fids as $fid) {
                 if ($file = File::load($fid)) {
                     $fids[] = $file->id();
                 }
             }
         }
     }
     $return['fids'] = $fids;
     return $return;
 }
 /**
  * {@inheritdoc}
  */
 public static function valueCallback(&$element, $input, FormStateInterface $form_state)
 {
     // Find the current value of this field.
     $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
     foreach ($fids as $key => $fid) {
         $fids[$key] = (int) $fid;
     }
     $force_default = FALSE;
     // Process any input and save new uploads.
     if ($input !== FALSE) {
         $input['fids'] = $fids;
         $return = $input;
         // Uploads take priority over all other values.
         if ($files = file_managed_file_save_upload($element, $form_state)) {
             if ($element['#multiple']) {
                 $fids = array_merge($fids, array_keys($files));
             } else {
                 $fids = array_keys($files);
             }
         } else {
             // Check for #filefield_value_callback values.
             // Because FAPI does not allow multiple #value_callback values like it
             // does for #element_validate and #process, this fills the missing
             // functionality to allow File fields to be extended through FAPI.
             if (isset($element['#file_value_callbacks'])) {
                 foreach ($element['#file_value_callbacks'] as $callback) {
                     $callback($element, $input, $form_state);
                 }
             }
             // Load files if the FIDs have changed to confirm they exist.
             if (!empty($input['fids'])) {
                 $fids = [];
                 foreach ($input['fids'] as $fid) {
                     if ($file = File::load($fid)) {
                         // Temporary files that belong to other users should never be
                         // allowed. Since file ownership can't be determined for anonymous
                         // users, they are not allowed to reuse temporary files at all.
                         if ($file->isTemporary() && (\Drupal::currentUser()->isAnonymous() || $file->getOwnerId() != \Drupal::currentUser()->id())) {
                             $force_default = TRUE;
                             break;
                         } else {
                             $fids[] = $file->id();
                         }
                     }
                 }
             }
         }
     }
     // If there is no input or if the default value was requested above, use the
     // default value.
     if ($input === FALSE || $force_default) {
         if ($element['#extended']) {
             $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : [];
             $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []];
         } else {
             $default_fids = isset($element['#default_value']) ? $element['#default_value'] : [];
             $return = ['fids' => []];
         }
         // Confirm that the file exists when used as a default value.
         if (!empty($default_fids)) {
             $fids = [];
             foreach ($default_fids as $fid) {
                 if ($file = File::load($fid)) {
                     $fids[] = $file->id();
                 }
             }
         }
     }
     $return['fids'] = $fids;
     return $return;
 }
function bootstrap_theme_dashboard_contribute_create_form_submit(&$form, &$form_state)
{
    global $user;
    $contribution = new stdClass();
    $contribution->type = NODE_TYPE_CLAS_CONTRIBUTOR;
    $contribution->status = NODE_NOT_PUBLISHED;
    $contribution->language = LANGUAGE_NONE;
    $contribution->uid = $user->uid;
    $term = taxonomy_term_load($form_state['values']['cno_learning_object_type']);
    $termname = $term->name;
    if (!empty($form_state['values']['cno_associated_materials'])) {
        $file = file_load($form_state['values']['cno_associated_materials']);
    } else {
        $file = file_managed_file_save_upload($form['cno_associated_materials']);
    }
    if (!empty($form_state['values']['cnob_photo'])) {
        $photo = file_load($form_state['values']['cnob_photo']);
    } else {
        $photo = file_managed_file_save_upload($form['cnob_photo']);
    }
    if ($termname == 'Audio') {
        if (!empty($form_state['values']['cno_lobj_res_audio'])) {
            $audio_file = file_load($form_state['values']['cno_lobj_res_audio']);
            //$fname_arr = explode("://",$audio_file->uri);
            //file_move($audio_file, "public://".$fname_arr[1], FILE_EXISTS_RENAME);
            $contribution->field_cnob_learn_obj_res_audio[$contribution->language][0] = array('fid' => $audio_file->fid, 'display' => 1);
        } else {
            $audio_file = file_managed_file_save_upload($form['cno_lobj_res_audio']);
        }
    }
    /*if (!empty($form_state['values']['cno_lobj_res_video'])) {
            $video_file = file_load($form_state['values']['cno_lobj_res_video']);
    		$contribution->field_cnob_learn_obj_res_video[$contribution->language][0] = array('fid' => $video_file->fid,'display' => 1);
        } else {
            $video_file = file_managed_file_save_upload($form['cno_lobj_res_video']);
        }*/
    if ($termname == 'Document') {
        if (!empty($form_state['values']['cno_lobj_res_document'])) {
            $doc_file = file_load($form_state['values']['cno_lobj_res_document']);
            $contribution->field_cnob_learn_obj_res_doc[$contribution->language][0] = array('fid' => $doc_file->fid, 'display' => 1);
        } else {
            $doc_file = file_managed_file_save_upload($form['cno_lobj_res_document']);
        }
    }
    if ($termname == 'Link') {
        if (!empty($form_state['values']['cno_lobj_res_link'])) {
            $res_link = $form_state['values']['cno_lobj_res_link'];
        } else {
            $res_link = '';
        }
        $contribution->field_cnob_learn_obj_res_link[$contribution->language][0]['value'] = $res_link;
    }
    $contribution->title = $form_state['values']['title'];
    $contribution->body[$contribution->language][0]['value'] = $form_state['values']['body'];
    $contribution->field_cnob_user_type[$contribution->language][0]['value'] = $form_state['values']['cno_type'];
    $contribution->field_cnob_category[$contribution->language][0]['tid'] = $form_state['values']['cno_category'];
    $contribution->field_cnob_learning_object_type[$contribution->language][0]['tid'] = $form_state['values']['cno_learning_object_type'];
    if ($termname == 'Video') {
        $contribution->field_cnob_learn_obj_res_video[$contribution->language][0]['video_url'] = $form_state['values']['cno_lobj_res_video'];
    }
    $contribution->field_cnob_area_of_study[$contribution->language][0]['tid'] = $form_state['values']['cno_area_of_study'];
    $contribution->field_cnob_grade_level[$contribution->language][0]['tid'] = $form_state['values']['cno_grade_level'];
    $contribution->field_cnob_expiration_date[$contribution->language][0]['value'] = date('Y-m-d', strtotime($form_state['values']['expiration_date']));
    $contribution->field_cnob_collections[$contribution->language][0]['nid'] = $form_state['values']['og_group_ref'];
    $contribution->og_group_ref[$contribution->language][0]['target_id'] = $form_state['values']['og_group_ref'];
    $contribution->field_cnob_content_area[$contribution->language][0] = array('tid' => $form_state['values']['cno_content_area']);
    $contribution->group_content_access[$contribution->language][0]['value'] = OG_CONTENT_ACCESS_PRIVATE;
    $contribution->cnob_content_area[$contribution->language][0]['value'] = OG_CONTENT_ACCESS_PRIVATE;
    //$contribution->field_cnob_relevant_standards[$contribution->language][0]['tid'] = $form_state['values']['cno_relevant_standards'];
    $contribution->field_cnob_lesson_plan[$contribution->language][0]['value'] = $form_state['values']['lesson_plan'];
    $contribution->field_cnob_assigned_badge[$contribution->language][0]['value'] = $form_state['values']['cno_badge'];
    if ($file) {
        $contribution->field_cnob_associated_materials[$contribution->language][0] = array('fid' => $file->fid, 'display' => 1);
    }
    if ($photo) {
        $contribution->field_cnob_photo[$contribution->language][0] = array('fid' => $photo->fid, 'display' => 1);
    }
    $contribution->field_cnob_comments[$contribution->language][0]['value'] = $form_state['values']['comments'];
    //////Tags////
    $tags = $form_state['values']['tags'];
    $typed_terms = drupal_explode_tags($tags);
    $tags_vocabulary = taxonomy_vocabulary_load(TAXONOMY_VOCABULARY_ID_TAGS);
    $value_tags = array();
    foreach ($typed_terms as $typed_term) {
        if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array(TAXONOMY_VOCABULARY_ID_TAGS)))) {
            $term = array_pop($possibilities);
        } else {
            $vocabulary = $tags_vocabulary;
            $term = array('tid' => 'autocreate', 'vid' => $vocabulary->vid, 'name' => $typed_term, 'vocabulary_machine_name' => $vocabulary->machine_name);
        }
        $value_tags[] = (array) $term;
    }
    $contribution->field_cnob_tags[$contribution->language] = $value_tags;
    //////Tags////
    //////Relevant Standards////
    $relevant_standards = $form_state['values']['cno_relevant_standards'];
    $typed_terms = drupal_explode_tags($relevant_standards);
    $rs_vocabulary = taxonomy_vocabulary_load(TAXONOMY_VOCABULARY_ID_RELEVANT_STANDARDS);
    $value_rs = array();
    foreach ($typed_terms as $typed_term) {
        if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array(TAXONOMY_VOCABULARY_ID_TAGS)))) {
            $term = array_pop($possibilities);
        } else {
            $vocabulary = $rs_vocabulary;
            $term = array('tid' => 'autocreate', 'vid' => $vocabulary->vid, 'name' => $typed_term, 'vocabulary_machine_name' => $vocabulary->machine_name);
        }
        $value_rs[] = (array) $term;
    }
    $contribution->field_cnob_relevant_standards[$contribution->language] = $value_rs;
    //////Relevant Standards////
    $userDetails = user_load($user->uid);
    $userRoles = $userDetails->roles;
    if (in_array('administrator', $userRoles) || in_array('editor', $userRoles)) {
        $contribution->field_cnob_is_featured[$contribution->language][0]['value'] = $form_state['values']['featured'];
    }
    $contribution->field_cnob_contributor[$contribution->language][0]['uid'] = $form_state['values']['cno_contributor'];
    node_object_prepare($contribution);
    //node_submit($contribution);
    //bootstrap_theme_node_presave($contribution);
    node_save($contribution);
    drupal_set_message('Created new contribution successfully.');
}