0

I have a rather odd problem on Drupal 7. I used the following code to create a new content type with module installation, add some fields and field instances. It all works good until I want to manage the display of the content type. White screen is shown as it eats 512MB of memory.

Interestingly, it manages to work on Rubik admin theme, but not on Seven, and this makes this problem even more weird!?

/**
 * Implements hook_install
 */
function mymodule_install() {
  $t = get_t();
  $type = 'sample_content_type';
  $types = [
    [
      'type'        => $type,
      'name'        => st('Sample content type'),
      'base'        => 'node_content',
      'description' => st(
        'Sample description'
      ),
      'custom'      => 1,
      'modified'    => 1,
      'locked'      => 0
    ],
  ];

  // create content type 
  foreach ($types as $type) {
    $type   = node_type_set_defaults($type);
    $status = node_type_save($type);

    if ($status == SAVED_NEW) { // create case
      watchdog(
        'node', 'Added content type %name.', ['%name' => $type->name], WATCHDOG_NOTICE,
        l($t('view'), 'admin/structure/types')
      );
    }
  }


  $fields = [
    [
      'field_name'  => 'field_mymodule_image',
      'type'        => 'image',
      'cardinality' => 1,
      'locked'      => FALSE,
      'indexes'     => array('fid' => array('fid')),
      'settings'    => array(
        'uri_scheme'    => 'public',
        'default_image' => FALSE,
      ),
      'storage'     => array(
        'type'     => 'field_sql_storage',
        'settings' => array(),
      ),
      'label'       => 'Image',
    ],
    [
      'field_name'  => 'field_mymodule_logo',
      'type'        => 'image',
      'cardinality' => 1,
      'locked'      => FALSE,
      'indexes'     => array('fid' => array('fid')),
      'settings'    => array(
        'uri_scheme'    => 'public',
        'default_image' => FALSE,
      ),
      'storage'     => array(
        'type'     => 'field_sql_storage',
        'settings' => array(),
      ),
      'label'       => 'Logo',
    ],
    [
      'field_name'  => 'field_mymodule_file',
      'type'        => 'file',
      'cardinality' => 1,
      'locked'      => FALSE,
      'indexes'     => array('fid' => array('fid')),
      'settings'    => array(
        'uri_scheme'      => 'public',
        'display_field'   => 0,
        'display_default' => 0,
      ),
      'storage'     => array(
        'type'     => 'field_sql_storage',
        'settings' => array(),
      ),
      'label'       => 'File',
    ],
    [
      'field_name'  => 'field_mymodule_link',
      'type'        => 'link_field',
      'module'      => 'link',
      'cardinality' => 1,
      'locked'      => FALSE,
      'settings'    => array(
        'attributes'      => array(
          'target' => 'default',
          'class'  => '',
          'rel'    => '',
        ),
        'url'             => 0,
        'title'           => 'optional',
        'title_value'     => '',
        'title_maxlength' => 128,
        'enable_tokens'   => 1,
        'display'         => array(
          'url_cutoff' => 80,
        ),
      ),
      'storage'     => array(
        'type'     => 'field_sql_storage',
        'settings' => array(),
      ),
      'label'       => 'Link',
    ],
  ];

  // create fields
  foreach ($fields as $field) {
    if (!field_info_field($field['field_name'])) {
      $new_field = field_create_field($field);
    }

    if(isset($new_field)){
      watchdog(
        'node',
        'Created field %name', ['%name' => $field['field_name']], WATCHDOG_NOTICE
      );
    }
  }

  $instances = [
    [
      'field_name' => 'field_othermodule_reference',
      'entity_type' => 'node',
      'bundle' => 'sample_content_type',
      'label'      => 'Some node eference',
    ],
    [
      'field_name'  => 'field_mymodule_image',
      'entity_type' => 'node',
      'bundle' => 'sample_content_type',
      'label'       => 'Image',
      'settings'    => [
        'file_directory' => 'node/sample_content_type/images',
        'file_extensions' => 'gif jpg jpeg png',
        'alt_field' => 1,
        'title_field' => 1,
        'default_image' => 0,
      ],
    ],
    [
      'field_name'  => 'field_mymodule_logo',
      'entity_type' => 'node',
      'bundle' => 'sample_content_type',
      'label'       => 'Logo',
      'settings'    => [
        'file_directory' => 'node/sample_content_type/logos',
        'file_extensions' => 'gif png',
        'alt_field' => 1,
        'title_field' => 1,
        'default_image' => 0,
      ],
    ],
    [
      'field_name'  => 'field_mymodule_file',
      'entity_type' => 'node',
      'bundle' => 'sample_content_type',
      'label'       => 'File',
      'settings'    => [
        'file_directory' => 'node/sample_content_type/files',
        'file_extensions' => 'txt pdf doc docx gif jpg jpeg png',
      ],
    ],
    [
      'field_name'  => 'field_mymodule_link',
      'entity_type' => 'node',
      'bundle' => 'sample_content_type',
      'label'       => 'Link',
    ],
  ];

  // create instances
  foreach ($instances as $instance) {

    if (!field_info_instance($instance['entity_type'],$instance['field_name'], $instance['bundle'])) {
      $new_instance = field_create_instance($instance);
    }

    if(isset($new_instance)){
      watchdog(
        'node',
        'Created instance of field %name', ['%name' => $instance['field_name']], WATCHDOG_NOTICE
      );
    }
  }
}

What am I missing?

2
  • Have you tried checking error logs? Or you can also set display_errors = On in php.ini. I'd start from there and trace the error.
    – Beebee
    Jun 24, 2015 at 14:01
  • Yep, that's the first thing i did and that's the way i found out that it drains out the memory as it's the only thing in log. PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 46066019 bytes) in /var/www/my_project/includes/theme.inc on line 1528, referer: my_project.loc/admin/structure/types Jun 24, 2015 at 14:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.