Example #1
0
/**
 * Helper function : given a node, return the list of referrers the
 * current user is allowed to create as an array defining :
 *
 * 'name' : Name of the referrer field
 * 'field' : Details of the referer field
 * 'referrence' : Detail of the reference field
 *
 */
function _nodereferrer_create_get_referrers($node)
{
    $list = array();
    if (!variable_get('nodereferrer_create_show_on_nodereferrer', 1)) {
        return $list;
    }
    $type = content_types($node->type);
    foreach ($type['fields'] as $name => $field) {
        if ($field['type'] != 'nodereferrer' || !is_array($field['referrer_types'])) {
            continue;
        }
        foreach (array_filter($field['referrer_types']) as $reftype) {
            if (!node_access('create', $reftype)) {
                continue;
            }
            $with_field = array();
            foreach (array_filter($field['referrer_fields']) as $reffield) {
                $cf = content_fields($reffield, $reftype);
                if ($cf) {
                    $with_field[$reftype][] = $cf;
                }
            }
            if (count($with_field) == 0) {
                continue;
            }
            $list[] = array('name' => substr($name, strlen('field_')), 'field' => $field, 'referrence' => $with_field);
        }
    }
    return $list;
}
/**
 * Form to add to existing node
 *
 */
function _nodereferrer_create_add_form($form_state, $node, $referrer_field_idx, $referrer_type, $referrence_field_idx)
{
    $form = array();
    $type = content_types($referrer_type);
    $form['referrence'] = array('#type' => 'textfield', '#title' => t('Select @content_type you want to add @title to', array('@content_type' => $type['name'], '@title' => $node->title)), '#autocomplete_path' => 'nodereferrer_create/autocomplete/' . $referrer_type, '#default_value' => '', '#maxlength' => 1024);
    $form['submit'] = array('#name' => 'add', '#type' => 'submit', '#value' => t('Add', array('@title' => $node->title, '@content_type' => $type['name'])));
    $form['cancel'] = array('#name' => 'cancel', '#type' => 'submit', '#value' => t('Cancel'));
    return $form;
}
Example #3
0
/**
 * Form to add to existing node
 *
 */
function _nodereferrer_create_add_reference_form($form_state, $node, $reference_field_idx, $reference_type)
{
    $form = array();
    $multiple = $node->reference_fields[$reference_field_idx]['multiple'] == 1;
    $field_name = $node->reference_fields[$reference_field_idx]['field_name'];
    $type = content_types($reference_type);
    if ($multiple) {
        $form['referrence'] = array('#type' => 'textfield', '#title' => t('Select @content_type you want to add to @title', array('@content_type' => $type['name'], '@title' => $node->title)), '#autocomplete_path' => 'nodereference/autocomplete/' . $field_name, '#default_value' => '', '#maxlength' => 1024);
    } else {
        $form['referrence'] = array('#type' => 'textfield', '#title' => t('Select @content_type you want to use for @title. This will replace the existing value.', array('@content_type' => $type['name'], '@title' => $node->title)), '#autocomplete_path' => 'nodereference/autocomplete/' . $field_name, '#default_value' => '', '#maxlength' => 1024);
    }
    $form['submit'] = array('#name' => 'add', '#type' => 'submit', '#value' => t('Add'));
    $form['cancel'] = array('#name' => 'cancel', '#type' => 'submit', '#value' => t('Cancel'));
    return $form;
}
Example #4
0
/**
 * Form builder. 
 *
 * Example form which has one "normal" FAPI field, plus CCK fields
 * from a hypothetical content type called "article". 
 */
function MYMODULE_my_custom_form(&$form_state)
{
    // Add some normal FAPI item.
    $form['title'] = array('#type' => 'textfield', '#title' => 'Title', '#required' => true, '#default_value' => NULL, '#maxlength' => 255, '#weight' => -5);
    // Add special CCK form items.
    module_load_include('inc', 'content', 'includes/content.node_form');
    // Assume a hypothetical content type called "article"
    $type = content_types('article');
    // Go through each of its custom fields and add them to our form.
    foreach ($type['fields'] as $field_name => $field) {
        // If we wanted a specific field, we'd filter it here, by name:
        // if ($field_name == 'field_article_my_custom_field') { }
        // But for this example we're going to add them all.
        $form['#field_info'][$field['field_name']] = $field;
        $form += (array) content_field_form($form, $form_state, $field);
    }
    $form['submit'] = array('#value' => 'Submit', '#type' => 'submit');
    return $form;
}
<?php

// $Id: node-news.tpl.php 7510 2010-06-15 19:09:36Z sheena $
$fields = content_types($node->type);
if (!empty($fields) && !empty($fields['fields'])) {
    ?>
    <!-- Fields of <?php 
    print $node->type;
    ?>
: 
    <?php 
    foreach ($fields['fields'] as $field) {
        print $field['field_name'];
        ?>
    
    <?php 
    }
    ?>
        -->
<?php 
}
?>
<div id="node-<?php 
print $node->nid;
?>
" class="node <?php 
print $node_classes;
?>
">
  <div class="inner">
    
  <dl>
    <dt><?php 
    print l(t('Create Content'), 'node/add');
    ?>
</dt>
    <dd><?php 
    print t('Add new content to your OpenPublish site.');
    ?>
</dd>
  </dl>
<?php 
} else {
    ?>
  <?php 
    if (module_exists('content')) {
        $content_types = content_types();
        $output = '';
        foreach ($content_types as $content_type) {
            $menu_link = menu_get_item("node/add/" . $content_type['url_str']);
            if ($menu_link['access']) {
                $output .= '<dt>' . l($content_type['name'], $menu_link['path']) . '</dt>';
                if ($content_type['description']) {
                    $output .= '<dd>' . $content_type['description'] . '</dd>';
                }
            }
        }
        if ($output) {
            print '<strong>' . t('Create Content') . '</strong><dl>';
            print $output;
            print '</dl>';
        }
Example #7
0
 function set_content_encoding($ext)
 {
     $variants = content_types();
     foreach ($variants as $content_type) {
         if ($content_type['id'] == $ext && $content_type['encoding']) {
             header("Content-Encoding: " . $content_type['encoding']);
         }
     }
 }
 public function load(AbstractMetaModelFactory $factory, AbstractMetaModel $metamodel, array $filters = NULL, $finalAttempt)
 {
     LogHelper::log_notice(t('Creating Meta Model using Drupal Content Types...'));
     $datasetCounter = 0;
     $contentTypes = content_types();
     if (isset($contentTypes)) {
         foreach ($contentTypes as $contentTypeName => $contentType) {
             // preparing list of tables which could be supported by our code
             $supportedTables = NULL;
             foreach ($contentType['fields'] as $field) {
                 $fieldName = $field['field_name'];
                 if ($field['multiple'] > 0) {
                     $message = t('Multiple values are not supported yet: @contentTypeName.@fieldName', array('@contentTypeName' => $contentTypeName, '@fieldName' => $fieldName));
                     LogHelper::log_warn($message);
                     continue;
                     // UnsupportedOperationException
                 }
                 // preparing table name where the field is stored
                 $fieldStorage = $field['db_storage'];
                 switch ($fieldStorage) {
                     case CONTENT_DB_STORAGE_PER_CONTENT_TYPE:
                         $tableName = _content_tablename($field['type_name'], $fieldStorage);
                         break;
                     case CONTENT_DB_STORAGE_PER_FIELD:
                         break;
                         $tableName = _content_tablename($fieldName, $fieldStorage);
                     default:
                         $message = t("Unsupported storage type - '@fieldStorage' for the field: @fieldName", array('@fieldStorage' => $fieldStorage, '@fieldName' => $fieldName));
                         LogHelper::log_warn($message);
                         continue;
                         // UnsupportedOperationException
                 }
                 // calculating number of 'visible' suffixes
                 $visibleSuffixCount = 0;
                 foreach ($field['columns'] as $columnAttributes) {
                     if (isset($columnAttributes['views'])) {
                         if ($columnAttributes['views'] === TRUE) {
                             $visibleSuffixCount++;
                         }
                     } else {
                         $visibleSuffixCount++;
                     }
                 }
                 // generating fields for all 'visible' suffixes
                 foreach ($field['columns'] as $columnSuffix => $columnAttributes) {
                     if (isset($columnAttributes['views']) && $columnAttributes['views'] === FALSE) {
                         continue;
                     }
                     $supportedField = new stdClass();
                     // required flag
                     $supportedField->required = $field->required == 1;
                     // original name of the field
                     $supportedField->original_name = $fieldName;
                     // calculating name of database column
                     $supportedField->column = $fieldName . '_' . $columnSuffix;
                     // field name
                     if ($visibleSuffixCount === 1) {
                         $supportedField->name = $fieldName;
                     } else {
                         $supportedField->name = $supportedField->column;
                     }
                     if (isset($supportedTables[$tableName]->storage)) {
                         $previousStorage = $supportedTables[$tableName]->storage;
                         if ($fieldStorage != $previousStorage) {
                             $message = t("Inconsistent storage for '@tableName' table([@fieldStorage1, @fieldStorage2]) for the field: @fieldName", array('@tableName' => $tableName, '@fieldName' => $fieldName, '@fieldStorage1' => $previousStorage, '@fieldStorage2' => $fieldStorage));
                             LogHelper::log_warn($message);
                             continue;
                             // IllegalStateException
                         }
                     } else {
                         $supportedTables[$tableName]->storage = $fieldStorage;
                     }
                     $supportedTables[$tableName]->supportedFields[$supportedField->name] = $supportedField;
                 }
             }
             // preparing dataset source
             $datasetSource = new stdClass();
             $datasetSource->assembler->type = ContentTypeDatasetSourceAssembler::$DATASET_SOURCE_ASSEMBLER__TYPE;
             $datasetSource->assembler->config->drupal = $contentType;
             if (isset($supportedTables)) {
                 $datasetSource->assembler->config->supportedTables = $supportedTables;
             }
             // preparing & registering dataset
             $dataset = new DatasetMetaData();
             $dataset->name = $this->getDatasetName($contentTypeName);
             $dataset->description = $contentType['description'];
             $dataset->datasourceName = AbstractDrupalDataSourceQueryProxy::$DATASOURCE_NAME__DEFAULT;
             $dataset->source = $datasetSource;
             // FIXME Populate list of columns and mark the dataset as complete
             $dataset->registerColumn('nid')->key = TRUE;
             $metamodel->registerDataset($dataset);
             $datasetCounter++;
         }
     }
     LogHelper::log_info(t('Processed @datasetCount datasets', array('@datasetCount' => $datasetCounter)));
     return self::LOAD_STATE__SUCCESSFUL;
 }
 * Other approaches to handling the upgrade are documented in the issue
 * linked above. YMMV.
 *
 * Updated by Daniel Mundra, dmundra@uoregon.edu, 2014-05-29
 * I removed the bootstrap code as you can run this script with drush without
 * needing to bootstrap. Also drush allows you run the code for site in a
 * multi-site environment.
 */
$drush_args = drush_get_arguments();
$cck_type = $drush_args[2];
// To get rid of the NOTICES resulting from undefined HTTP environment vars.
error_reporting(E_ALL ^ E_NOTICE);
/**
 * Get all fields for $cck_type.
 */
$type = content_types($cck_type);
$fields = $type['fields'];
/**
 * Pick out all the fields of type 'text_textfield'.
 */
print "Getting all CCK fields of type 'text_textfield'...\n";
$text_fields = array();
foreach ($fields as $field) {
    if ($field['widget']['type'] == 'text_textfield') {
        $text_fields[] = $field['field_name'];
    }
}
/**
 * Get all nodes of type $cck_type.
 */
print "Getting all nodes of type '{$cck_type}'...\n";
 public function settingsForm(array $form, array &$form_state)
 {
     $options = array();
     if (\Drupal::moduleHandler()->moduleExists('content')) {
         $types = content_types();
         foreach ($types as $type) {
             $warn = '';
             if (count($type['fields'])) {
                 $warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
             }
             $options[$type['type']] = array('#markup' => t($type['name']) . $warn);
         }
     } else {
         $types = NodeType::loadMultiple();
         $comment_fields = $this->commentManager ? $this->commentManager->getFields('node') : array();
         $map = array(t('Hidden'), t('Closed'), t('Open'));
         foreach ($types as $type) {
             $options[$type->type] = array('type' => array('#markup' => t($type->name)));
             if ($this->commentManager) {
                 $fields = array();
                 foreach ($comment_fields as $field_name => $info) {
                     // Find all comment fields for the bundle.
                     if (in_array($type->type, $info['bundles'])) {
                         $instance = FieldInstanceConfig::loadByName('node', $type->type, 'comment');
                         $default_mode = reset($instance->default_value);
                         $fields[] = String::format('@field: !state', array('@field' => $instance->label(), '!state' => $map[$default_mode['status']]));
                     }
                 }
             }
             // @todo Refactor display of comment fields.
             if (!empty($fields)) {
                 $options[$type->type]['comments'] = array('data' => array('#theme' => 'item_list', '#items' => $fields));
             } else {
                 $options[$type->type]['comments'] = t('No comment fields');
             }
         }
     }
     if (empty($options)) {
         $this->setMessage(t('You do not have any content types that can be generated. <a href="@create-type">Go create a new content type</a> already!</a>', array('@create-type' => url('admin/structure/types/add'))), 'error', FALSE);
         return;
     }
     $header = array('type' => t('Content type'));
     if ($this->commentManager) {
         $header['comments'] = array('data' => t('Comments'), 'class' => array(RESPONSIVE_PRIORITY_MEDIUM));
     }
     $form['node_types'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options);
     $form['kill'] = array('#type' => 'checkbox', '#title' => t('<strong>Delete all content</strong> in these content types before generating new content.'), '#default_value' => $this->getSetting('kill'));
     $form['num'] = array('#type' => 'textfield', '#title' => t('How many nodes would you like to generate?'), '#default_value' => $this->getSetting('num'), '#size' => 10);
     $options = array(1 => t('Now'));
     foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
         $options[$interval] = \Drupal::service('date')->formatInterval($interval, 1) . ' ' . t('ago');
     }
     $form['time_range'] = array('#type' => 'select', '#title' => t('How far back in time should the nodes be dated?'), '#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'), '#options' => $options, '#default_value' => 604800);
     $form['max_comments'] = array('#type' => $this->moduleHandler->moduleExists('comment') ? 'textfield' : 'value', '#title' => t('Maximum number of comments per node.'), '#description' => t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'), '#default_value' => $this->getSetting('max_comments'), '#size' => 3, '#access' => $this->moduleHandler->moduleExists('comment'));
     $form['title_length'] = array('#type' => 'textfield', '#title' => t('Maximum number of words in titles'), '#default_value' => $this->getSetting('title_length'), '#size' => 10);
     $form['add_alias'] = array('#type' => 'checkbox', '#disabled' => !$this->moduleHandler->moduleExists('path'), '#description' => t('Requires path.module'), '#title' => t('Add an url alias for each node.'), '#default_value' => FALSE);
     $form['add_statistics'] = array('#type' => 'checkbox', '#title' => t('Add statistics for each node (node_counter table).'), '#default_value' => TRUE, '#access' => $this->moduleHandler->moduleExists('statistics'));
     $options = array(Language::LANGCODE_NOT_SPECIFIED => t('Language neutral'));
     if ($this->moduleHandler->moduleExists('locale')) {
         $languages = language_list();
         foreach ($languages as $langcode => $language) {
             $options[$langcode] = $language->name;
         }
     }
     $form['add_language'] = array('#type' => 'select', '#title' => t('Set language on nodes'), '#multiple' => TRUE, '#disabled' => !$this->moduleHandler->moduleExists('locale'), '#description' => t('Requires locale.module'), '#options' => $options, '#default_value' => array(Language::LANGCODE_NOT_SPECIFIED));
     $form['submit'] = array('#type' => 'submit', '#value' => t('Generate'), '#tableselect' => TRUE);
     $form['#redirect'] = FALSE;
     return $form;
 }
Example #11
0
 /**
  * Drupal 6
  */
 protected function readDrupal6SiteProperties()
 {
     $node_types = node_get_types();
     $fields = content_fields();
     foreach ($node_types as $bundle => $entity_info) {
         $field_types = content_types($bundle);
         $instances = array();
         foreach ($field_types['fields'] as $fieldName => $fieldInfo) {
             $instance = array('entity_type' => 'node', 'bundle' => $bundle, 'field_name' => $fieldName, 'label' => $fieldInfo['widget']['label'], 'description' => $fieldInfo['widget']['description'], 'widget' => array('type' => $fieldInfo['widget']['type'], 'settings' => array()));
             $instances[$fieldName] = $this->processInstance($instance);
         }
         $this->entityTypes[] = new NodeTypeBuilder($bundle, $entity_info->name, $entity_info->description, $instances);
     }
     $vocs = taxonomy_get_vocabularies();
     foreach ($vocs as $bundle => $entity_info) {
         $this->entityTypes[] = new VocabularyBuilder($entity_info->vid, $entity_info->name, $entity_info->description);
     }
     foreach ($fields as $fieldName => $fieldInfo) {
         $this->fields[] = new FieldBuilder($fieldName, $fieldInfo['type']);
     }
     $this->parseDefinition();
 }
<?php

/**
 * Based on my answer to: "How to get the first image field from a node without knowing the field's name?"
 * @see: http://stackoverflow.com/questions/6656595
 */
module_load_include('inc', 'content', 'includes/content.node_form');
$content_types = array('page', 'story', 'product', 'some_content_type');
$lightest_imagefields = array();
// arranged by content type
foreach ($content_types as $content_type_name) {
    $content_type_data = content_types($content_type_name);
    $last_weight = NULL;
    foreach ($content_type_data['fields'] as $field_name => $field_data) {
        if ($field_data['widget']['type'] == 'imagefield_widget' && (is_null($last_weight) || (int) $field_data['widget']['weight'] < $last_weight)) {
            $lightest_imagefields[$content_type_name] = $field_name;
            $last_weight = (int) $field_data['widget']['weight'];
        }
    }
}
/** Hypothetical Usage:
 * $node = load_some_node_i_want();
 * $node->$lightest_imagefields[$node->type]; // Access this node's lightest imagefield.
 */